Apps Script - Formatting the current date for timestamp - google-apps-script

I wish to format the current date as "yyyy/MM/dd HH:mm". formatDate allows me to format the date but I need to specify a timezone, per the following:
Utilities.formatDate(new Date(), "GMT", "yyyy/MM/dd HH:mm")
This is almost what I want, but I want the result to be BST or GMT, as appropriate to the calendar. What timezone should I specify to get the answer without further manipulation of the text date? (I do have a clumsy workaround). The problem caught me out with a timed batched process that runs at midnight upon our recent BST clock change where timestamps of 23:00 the previous day were applied.

In your script properties, make sure you've selected the correct time zone. Then, in your script, instead of using "GMT", use Session.getScriptTimeZone().
function test() {
var date1 = new Date("March 10, 2018 10:00");
var date2 = new Date("March 11, 2018 10:00");
Logger.log(date1); // Sat Mar 10 10:00:00 GMT-05:00 2018
Logger.log(Utilities.formatDate(date1, Session.getScriptTimeZone(), "yyyy/MM/dd HH:mm")); // 2018/03/10 10:00
Logger.log(date2); // Sun Mar 11 10:00:00 GMT-04:00 2018
Logger.log(Utilities.formatDate(date2, Session.getScriptTimeZone(), "yyyy/MM/dd HH:mm")); // 2018/03/11 10:00
}
In the script editor, go to File > Project properties and set your time zone. (I used Eastern time for mine.)

Related

getRange.getValue returning wrong date from Google sheets

var sheetdate = activeSheet.getRange(x, y).getValue() I was using this line to read a date from google sheets. Date in sheets is 2021-02-01. But sheetdate is returning value Sun Jan 31 13:30:00 GMT-05:00 2021. Actual output should be Mon Feb 1 17:35:00 GMT 05:30
This is an issue with the timezone as Sourcerer mentioned.
There are many possible answers to this one but I prefer this one, formatting the date using Utilities as you can control your output:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
date = sheet.getRange(1, 1).getValue()
Logger.log(date);
Logger.log(SpreadsheetApp.getActive().getSpreadsheetTimeZone());
Logger.log(Utilities.formatDate(date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "E MMM dd HH:mm:ss z yyyy"));
}
For the formatting, the one I used above "E MMM dd HH:mm:ss z yyyy" is trying to emulate the default date output. See the reference below and feel free to modify based on what you need to output for the date
Referenece:
SimpleDateFormat

Date changes when setValues() is called

I have written a script for a client in a Spreadsheet whose time is GMT-5 while my TimeZone is GMT+5. The column H on the spreadsheet's tab has a value as date without any time factor e.g. 01/25/2019.
On the start of script function I am setting the format of column like under the following .setNumberFormat("MM/dd/yyyy"). Also note that I am working with .getDisplayValues() which returns me test for the Date column.
There is nowhere in the script I am explicitly updating DateValue its value. After manipulating some other values on the other column I happen to update the row by calling the .setValues([row]).
Then the date changes to 01/24/2019 and when I change the format to include time factor then it is like 01/24/2020 19:00:00.
Interesting point is it does not occur on my end where my timeZone is GMT+5. Even though I changed the Spreadsheet's Timezone to GMT-5 and on my system as well but still it doesn't occur on my end but on the client machine.
The runtime version of the script is:
"runtimeVersion": "V8"
This can happen when the script time zone and spreadsheet time zone are different and you format the date object
I cannot reproduce your exact situation from the information you provided, but I invite you to do the following test:
Set your spreadsheet timezone to any timezone that is ahead of your script time zone
Set cell A1 of your active sheet to =today()
Run the following code:
function myFunction(){
var displayDate = SpreadsheetApp.getActiveSheet().getRange("A1").getDisplayValue();
Logger.log("displayDate: " + displayDate);
var date = SpreadsheetApp.getActiveSheet().getRange("A1").getValue();
Logger.log("actual date retrieved by Apps Script: " + date);
var timeZone = Session.getScriptTimeZone();
Logger.log("ScriptTimeZone: " + timeZone);
var timezone2 = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
Logger.log("SpreadsheetTimeZone: " + timezone2);
var newDate = Utilities.formatDate(date, timeZone, "MM/dd/yyyy");
Logger.log("formatted date that will be set back to the spreadsheet: " + newDate);
SpreadsheetApp.getActiveSheet().getRange("A1").setValue(newDate);
}
function updatePatientTransactions(transactionSheetValues, patientNam, insuranceName) {
var tempRow = [];
for(var i= start; i<= end-2; i++) {
tempRow = transactionSheetValues[i];
tempRow[0] = patientName;
tempRow[3] = insuranceName;
transactionSheet.getRange(i+2,1,1,transactionSheet.getLastColumn()).setValues([tempRow]);
}
}
Observe the logs and the output
Sample logs:
Stackdriver logs
Jun 8, 2020, 1:03:00 PM Info displayDate: 6/8/2020
Jun 8, 2020, 1:03:01 PM Info actual date retrieved by Apps Script: Sun Jun 07 2020 23:00:00 GMT+0200 (Central European Summer Time)
Jun 8, 2020, 1:03:01 PM Info ScriptTimeZone: Europe/Paris
Jun 8, 2020, 1:03:01 PM Info SpreadsheetTimeZone: Europe/Athens
Jun 8, 2020, 1:03:01 PM Info formatted date that will be set back to the spreadsheet: 06/07/2020
so the date will be set back to yesterday.
Why?
When you retrieve a value that is formatted as Date and not Date Time, that is if the time is not set - Apps Script automatically sets it to midnight.
When it is the 8th of June midnight in Athens, it is the 7th of June 23:00 o'clock in Paris
This is why the conversion from a spreadsheet timezone to a script timezone lying behind will lead to a date change
Now, if you format the date value as a date string or process it otherwise, the informaiton about the original timezone might get lost and when you set the datestring back to the spreadsheet, you will set back the Apps Script date - different from the spreadsheet date - so be careful!

google apss script time - logs pm time as am

I have this code:
var time = new Date();
time = Utilities.formatDate(time, "GMT+02", "dd/MM/yy hh:mm");
When I use the time parameter as a file name or log it into a spreadsheet cell - it logs the hours part as am only.
For example, both 03:20 am (03:20) and 03:20 pm (15:20) will be logged as 03:20.
I am trying to log it correctly in a military time format (15:20).
How can I do so?
You want to show 03:20 pm as 15:20.
If my understanding is correct, how about this modification?
From:
hh:mm
To:
HH:mm
Reference:
Date and Time Patterns
H: Hour in day (0-23)
h: Hour in am/pm (1-12)

Utilities.formatDate Date not set correct

I have a date stored as follows:
existingDate = Wed Apr 30 2014 00:00:00 GMT+0100 (BST)
When I use Utilities.formatDate to format the date the date is changed to the day before.
var formattedDate = Utilities.formatDate(new Date(existingDate), "GMT", "dd/MM/yyyy");
the formatted date is then set to 29/04/2014 and not 30/04/2014.
Has anyone else seen this behavior.
Utilities.formatDate seems to be working fine.
You have Apr 30 midnight in GMT-1 but then you tell it to format this date in a different timezone GMT, or more explicity GMT-0. The expected result is indeed Apr 29 23h.
The second parameter in Utilities.formatDate must be the timezone you desire.
There are some other issues with this google script.
If I want the timezone AEST but only have the date in the form of 1/27/2017, then Utilities.formatDate("1/27/2017","AEST","yyyy-MM-dd") will return a value of 2017-01-26, a day before it should be! However if I have a time with it such as "1/27/2017 12:00:00" then it returns the correct day. However if I use "GMT+11" as the timezone even without a time on the end if returns the correct day.
Example -
Logger.log('Example 1='+Utilities.formatDate(new Date("1/27/2017"),"AEST","yyyy-MM-dd"));
Logger.log('Example 2='+Utilities.formatDate(new Date("1/27/2017 12:00:00"),"AEST","yyyy-MM-dd"));
Logger.log('Example 3='+Utilities.formatDate(new Date("1/27/2017"),"GMT+11","yyyy-MM-dd"));
Output -
Example 1=2017-01-26
Example 2=2017-01-27
Example 3=2017-01-27
A workaround when reading a date from a spreadsheet cell that does not have a time is to add a time and when using a timezone like "AEST" or similar, something like this -
var displayDate = sheet.getRange(a1Notation).getDisplayValue(); // Not .getValue();
Logger.log(Utilities.formatDate(new Date(displayDate + " 12:00:00"), "AEST", "yyyy-MM-dd"));
However "GMT+11" would be easier because you don't need to add a time.

Google SpreadSheet Custom Date Format

I'm looking for a easy formula to generate this type of Date Format:
10:35 PM August 12th 2013
hh:mm AM/PM Month dd yyyy
It's for the MarketMeSuite CSV scheduling utility that only excepts that format when uploading scheduled tweets.
Yes this format is possible using the Utilities.formatDate() utility. Please see Oracles Class SimpleDateFormat documentation for java as this uses the same formats. You should be able to get your desired format using the below formatting.
h:mm a MMMMM d yyyy
The only issues would be with the th or nd or rd or st after the date. But you could use some if statements to format that depending on the number, I'll let you work on that :)
The final string of the above would be -
9:16 PM April 15 2013
So the final date object creation would be.
Utilities.formatDate(new Date(), "GMT", "h:mm a MMMMM d yyyy")
EDIT
I've just realised you might not be talking about getting this format in a script but actually just calling it in a cell. If so you still need to create a custom function, so add the below code to a new script.
function getDate() {
var date = Utilities.formatDate(new Date(), "GMT", "h:mm a MMMMM d yyyy")
return date;
}
Then you can call it back in a cell with =getDate()