This is driving me nuts, but its got to be something simple:-)
I have a Google script running on a spreadsheet, and I want to enter a string that includes the current date (eg "Produced at: 06 Oct 2016 @ 18:10").
The test code I'm using and an example Logger.log is below.
My problem is that the result from Utilities.Formatdate always returns the same value regardless of the actual time.
Any ideas guys? TIA:-) Cheers ...Steve
function TEST() {
var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell=sheet.getRange("A1");
Logger.log(new Date());
var nowdate = new Date();
Logger.log (nowdate);
var nowdatestr = ("Produced at: " +Utilities.formatDate(nowdate, "GMT+10", "dd MMM YYYY @ HH:MM"));
Logger.log (nowdatestr);
// cell.setValue(nowdatestr);
Logger.log (Utilities.formatDate(new Date(), "GMT+10", "dd MMM YYYY @ HH:MM"));
Logger.log (cell.getValues());
Logger.log(new Date());
}
Log results:
[16-10-06 18:46:58:022 AEST] Thu Oct 06 18:46:58 GMT+10:00 2016
[16-10-06 18:46:58:022 AEST] Thu Oct 06 18:46:58 GMT+10:00 2016
[16-10-06 18:46:58:024 AEST] Produced at: 06 Oct 2016 @ 18:10
[16-10-06 18:46:58:024 AEST] 06 Oct 2016 @ 18:10
[16-10-06 18:46:58:081 AEST] [[Title]]
[16-10-06 18:46:58:082 AEST] Thu Oct 06 18:46:58 GMT+10:00 201
+++++++++++Added following posts from Vytautas and Ruben.
@Vytautas: Your tidied up version omits the log of the real current date, which then shows the problem I'm having.
So using:
function TEST2() {
var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell=sheet.getRange("A1");
var nowdate = new Date()
var nowdatestr = ("Produced at: " + Utilities.formatDate(nowdate, 'GMT+10', 'dd MMM YYYY @ HH:MM'));
Logger.log (nowdatestr);
cell.setValue(nowdatestr);
Logger.log ("real new date and time is: " +new Date());
Logger.log (cell.getValues())
}
Gives the Log:
[16-10-07 12:31:57:265 AEST] Produced at: 07 Oct 2016 @ 12:10
[16-10-07 12:31:57:315 AEST] real new date and time is: Fri Oct 07 2016 12:31:57 GMT+1000 (AEST)
[16-10-07 12:31:57:448 AEST] [[Produced at: 07 Oct 2016 @ 12:10]]
@Ruben: Inserting Utilities.sleep(1000) before each new Date() call made no difference.
Maybe it's not something simple as I first thought...:-)
++++++++++++++++++
The final working test code (thanks Ruben) is (change HH:MM to HH:mm):
function TEST2() {
var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell=sheet.getRange("A1");
var nowdate = new Date()
var nowdatestr = ("Produced at: " + Utilities.formatDate(nowdate, 'GMT+10', 'dd MMM YYYY @ HH:mm'));
Logger.log (nowdatestr);
cell.setValue(nowdatestr);
Logger.log ("real new date and time is: " +new Date());
Logger.log (cell.getValues())
}
Thanks!
Instead of HH:MM
use HH:mm
According to http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html:
function myFunction() {
// General variables
var stringTimeZone = 'GMT-0500';
var stringFormat = 'dd MMM YYYY @ HH:mm'; // Use m for minutes, M for months
// Main code
var dateOne = new Date(); // First date instance
var stringOne = Utilities.formatDate(dateOne, stringTimeZone , stringFormat);
Logger.log(stringOne);
Utilities.sleep(1000*60); // Wait one minute in order to force different date-times
var dateTwo = new Date(); // Seconde date instance
var stringTwo = Utilities.formatDate(dateTwo, stringTimeZone, stringFormat);
Logger.log(stringTwo);
}
When I ran the above script, the resulting logs were the following:
[16-10-06 20:43:38:717 PDT] 06 Oct 2016 @ 22:43
[16-10-06 20:44:38:750 PDT] 06 Oct 2016 @ 22:44