To preface I'm very new at this but I currently have a script that outputs a Google Doc/PDF based on inputs into my google form.
The PDF is output whenever a new form is submitted, currently the formatting for the date comes out as DD/MM/YY but I want it to appear as dd MMMM yyyy i.e. 19 May 2020 rather than 19/05/20.
I've tried using the following to change the formatting
var signDate = e.values[1];
var formatSignDate = Utilities.formatDate(signDate, "GMT+1", "dd MMMM yyyy");
body.replaceText('{{Execution Date}}', formatSignDate);
But I get the following error
Exception: The parameters (String,String,String) don't match the method signature for Utilities.formatDate.
at autoFillFromForm(Code:17:28)
What am I doing wrong? Can I not use .formatDate with the date from my sheets cells?
Edit: error message with log of signDate
Stackdriver logs Info signDate = 20/05/2020 Error Exception: The parameters
(String,String,String) don't match the method signature for
Utilities.formatDate. at autoFillFromForm(Code:23:34)
Exception: The parameters (String,String,String) don't match the method signature for Utilities.formatDate
means that signDate is not recognized correctly as a date object
Please provide a log of signDate to help you troubleshoot more in detail.
UPDATE
If you have a date string in UK format ( DD/MM/YYYY), it won't be automatically recognized as a Javascript date object.
You'll need to convert it, e.g:
var convertedDate = new Date(signDate.split('/')[2], signDate.split('/')[1] - 1, signDate.split('/')[0]);
Logger.log(convertedDate);
var formatSignDate = Utilities.formatDate(convertedDate, "GMT+1", "dd MMMM yyyy");
Logger.log(formatSignDate);
Note: Using Utilities.formatDate() as above might give you the wrong date if you are setting it to a different timezone than your script time zone.
Related
When parsing date and time from an API call which is formatted like this 2018 - 04 - 18T21:51:00 I get and error that states
and I don't understand what am I doing wrong.
/// I set in which format should my data be, and parse the data from the API call
Text('Date and Time:
${DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.parse(apicall.timeanddate))}'),
After that I would format the Date and Time according to Locale language preference of the device.
Any help is appreciated.
The format you are passing is invalid, so you need to transform it:
String getDateTime(String dateFromServer){
dateFromServer = dateFromServer.replaceAll(" ","");
dateFromServer = dateFromServer.replaceAll("T"," ");
return dateFromServer;
}
I replaced all spaces and T that were causing problems you can now use this with:
DateFormat("yyyy-MM-dd hh:mm:ss").format(getDateTime(apicall.timeanddate));
Hope this would solve your problem.
I have a component that uses a datepicker. The datepicker needs a dateFormat property that fits the momentjs pattern, for example 'DD.MM.YYYY' or 'MM/DD/YYYY'.
The date formatting is handled by react-intl. This works fine when converting from a date to a string (via formatDate). However, I need to retrieve the pattern as described above.
My goal is to do something like
dateFormat = this.props.intl.extractDateFormat() // returns 'DD.MM.YYYY'
I have found this similar question, but the only answer relies on parsing the string, which I cannot do, because I do not know whether Day or Month will come first in the formatted date.
If it is possible to convert this string to a date and somehow retrieve the format from momentjs, that would also be a good solution.
I was able to get the date format from react-intl. To do this, I defined an example date and had it formatted by react-intl, and then parsed the format by referring to the original string.
My component which is exported as injectIntl(Component) has this method:
deriveDateFormat = () => {
const isoString = '2018-09-25' // example date!
const intlString = this.formatDate(isoString) // generate a formatted date
const dateParts = isoString.split('-') // prepare to replace with pattern parts
return intlString
.replace(dateParts[2], 'DD')
.replace(dateParts[1], 'MM')
.replace(dateParts[0], 'YYYY')
}
The date will e.g. be formatted to '09/25/2018', and this function would return 'MM/DD/YYYY', a format which can be used by Moment.js.
This function only works if you know that the month and day will always be displayed with two digits. It would fail if the format is something like 9/25/2018.
I have not found a way to extract the date format from react-intl directly.
What is the best way to determine the correct date format (dd/mm or mm/dd) of a Google Form response.
When I use the namedValues object:
function onFormSubmit(e){
var namedValues = e.namedValues;
var date = namedValues['Date']; // Date=[05/06/2018]
var date = new Date(date);
Logger.log(date); //Sun May 06 00:00:00 GMT+10:00 2018
}
When I use the value from the spreadsheet:
function onFormSubmit(e){
var range = e.range;
var row = range.getRow();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form Responses 1');
var date = sheet.getRange(row,2).getValue();
Logger.log(date); //Mon Jun 05 00:00:00 GMT+10:00 2018
}
I don't know whether I should be, but I am hesitant using the values from the spreadsheet in an onFormSubmit trigger, since I have experienced instability in the past where I think the trigger was running before the data was being posted to the spreadsheet.
I cannot find anything in the Google Forms documentation stating whether a date response is always in a consistent format. If it was always dd/mm/yyyy I could construct the date using the string parts.
Is there a way to use determine the correct date format from the namedValues object?
P.S I would rather not use the moment.js library for this one requirement, so keen to understand if its possible without.
You Google form will constantly submit the date in the same format, but your sheet might change the appearance of formatting.
Also, there is now a date format utility built into Apps script. You can change the date string to the format that you need.
// This formats the date as Greenwich Mean Time in the format
// year-month-dateThour-minute-second.
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
Logger.log(formattedDate);
I have been struggling with this even after doing so much of research on such a simple thing, so I need some help here.
I need to pass current date in date data type only in 'yyyy-MM-dd' format. SimpleDateFormat converts current date to string type and while trying to parse though it gets converted to Date type but changes the format.
I need currentDate in the format 'yyyy-MM-dd' of Date Type.
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", currentDate)
I have managed to figure out a solutions to this. First got my desired format which obviously converted it to a String and then parsed this to a Date. It has worked perfectly fine.
SimpleDateFormat nsdf = new SimpleDateFormat('yyyy-MM-dd')
String currentDate = new SimpleDateFormat('yyyy-MM-dd').format(new Date());
Date newDate = (Date)nsdf.parse(currentDate)
if(!session.dtfromDate && !session.dttoDate)
eq("startDate", newDate)
I need t use the CQ5 xtype 'datefield' since I need only the date and not time tombe entered by the author.
But the issue is that 'datefield' saves the date in JCR as a String and not as a timestap [ as it does when 'datetime' is used.]
Is there a work around to save the date as a timestamp ?
I don't think it is possible to save the date as timestamp using datefield, without meddling with the default script. But as a workaround, you can use datetime and set the property hideTime to true, to hide the time part, so that the author will not be able to author it.
The json for the config is shown below.
{ "fieldLabel":"Date",
"xtype":"datetime",
"hideTime":true,
"name":"./date",
"defaultValue":"now",
"jcr:primaryType":"cq:Widget"
}
You can add defaultValue to 'now', if you want current date to be initialized as the default, if not explicitly filled in by the author, else it can be ignored.
NOTE: The defaultValue: 'now' doesnt work for me in IE (i am using IE 11 and emulating the previous versions through dev tools), but it works fine in Chrome and Mozilla.
A rough workaround for your jsp:
<%#page import="java.text.SimpleDateFormat,java.util.Date"%>
<%
SimpleDateFormat displayDateFormat = new SimpleDateFormat("dd MMM yyyy");
String dateField = properties.get("nameofdatefield", "");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
Date formattedDate = sdf.parse(dateField);
String formattedDateStr = displayDateFormat.format(formattedDate);
out.println('Example of formated string'+formattedDateStr);
%>
From the above, you can also convert it to a Date Object, depending on what you wish to manipulate.
Let me know if the above example helps