Utilities function in Google App Scripts not working - google-apps-script

Function Utilities.formatDate in Google App Scripts is not working correct for date in year 2013
Example-
date = Tue Dec 31 2013 18:43:12 GMT+0530 (IST)
after formatting it in YYYYMMdd format
using code-
Utilities.formatDate(date, "IST" ,"YYYYMMdd"))
result was- 20**14**1231
In the above result year is expected to be 2013 as per above mentioned date.
The same code is working correct for date in 2012 and 2014.

Just change your pattern from YYYY to yyyy (lower case) and it will work, check this:
function myFunction() {
var date = new Date("Tue Dec 31 2013 18:43:12 GMT+0530 (IST)");
//after formatting it in YYYYMMdd format
var format = Utilities.formatDate(date,"IST", "yyyyMMdd");
Logger.log(format);
}

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

google app script how to get date in string date(spread sheet)

I want to compare the date that was created in the spreadsheet with a specific date.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var spreaddate = ss.getSheetByName("sheet123").getRange("B2").getValue();
//--> Thu Dec 12 00:00:00 GMT+09:00 2019
var comparedate = new date(yyyy-mm-dd)//somting like this
if(spreaddate > comparedate){
do something
}
The spreaddate is not recognized as a date.
Even if I try to convert it to a date, it's hard because it's written in letters, not numbers like Thu Dec.
What should I do?
I think the problem is that you're not correctly defining your comparedate. You need to use a capital 'D' new Date().

How to format Date in Google Script

I am trying to write a small Google Script which takes data from some cells of Google Sheet and Paste it in the GMAIL.
However, while pasting the 'Date Values' it always displays it in the following manner:-
Mon Apr 15 2019 00:00:00 GMT+0530 (IST)
Fri Apr 26 2019 00:00:00 GMT+0530 (IST)
But I need the dates in an appropriate way i.e.
"Mon Apr 15 2019" or
"Fri 04/26/2019"
I gone through these possible options i.e. Utilities.formatDate & .Split but somehow I am not able to write these codes appropriately. Can you please help me with this matter. Below I have mentioned the entire issue in detail.
My Code
function temp2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheetByName('Sheet1');
var templateSheet = ss.getSheetByName('Sheet2');
var emailTemplate2 = templateSheet.getRange("G1").getValue();
var rec = templateSheet.getRange("C1").getValue();
var sub = templateSheet.getRange("D1").getValue();
var date = templateSheet.getRange("E1").getValue();
// Logger.log(rec);
MailApp.sendEmail(rec, sub, emailTemplate2);
}
here var date = templateSheet.getRange("E1").getValue(); is the part of code which picks value of date.
Do let me know if you need more details in this regard
Regards,
Alok
Requirement:
Format date value from cell in Google Apps Script.
Solution:
Pass value to date constructor new Date() then format using Utilities.formatDate().
// pass date to date constructor
var date = new Date(templateSheet.getRange("E1").getValue());
// "Mon Apr 15 2019" example
var formattedDate = Utilities.formatDate(date, "GMT+0", 'E MMM dd yyyy');
// "Fri 04/26/2019" example
var formattedDate = Utilities.formatDate(date, "GMT+0", 'E MM/dd/yyyy');
Note: I've set this to timezone "GMT+0" by default, you can change this to whichever time zone you need.
References:
new Date() for date constructor.
Utilities.formatDate() for formatting dates in Google Apps Script.
SimpleDateFormat for date format strings.

Apps Script - Formatting the current date for timestamp

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.)

Using dates in Google Sheets

Dates are giving a bit of trouble in Google Sheets. I'm basically trying to subtract dates but it's not working. However first, I'm trying to understand how Dates work. Here's an example of an odd behavior with dates.
function addDates(sheet)
{
var prevDateCurrYear = new Date();
Logger.log(prevDateCurrYear);
Logger.log(prevDateCurrYear.getMonth()+' '+prevDateCurrYear.getDay()+' '+prevDateCurrYear.getYear());
}
This is the Log
[15-02-03 18:15:21:631 EST] Tue Feb 03 18:15:21 GMT-05:00 2015
[15-02-03 18:15:21:632 EST] 1 2 2015
The getMonth and getDay doesn't seem to work. It should be pulling 2 and 3 but instead pulls 1 and 2. Why is this happening?
I'm using this documentation: https://developers.google.com/apps-script/reference/contacts/date-field
Actually getMonth and getDay are Java script methods. The way the getMonth and getDay works different in Javascript. You can refer to these pages for more information.
http://www.w3schools.com/jsref/jsref_getday.asp
http://www.w3schools.com/jsref/jsref_getmonth.asp
Hope that helps!
Couple things to consider.
1. Set the Time Zone in your script Project Properties. File, Project Properties.
2. Using Utilities.formatDate() can relieve a lot of headaches as you can standardize your dates to your needs.
3. getDay() and getDate() return "day of the week (i.e. 1-7)" and "the date of the month (i.e. 1-31)", respectively.
here is some more details about dates to clarify:
function addDates() {
var prevDateCurrYear = new Date();
var year = Utilities.formatDate(prevDateCurrYear, "America/Denver", "yyyy");
var date = Utilities.formatDate(prevDateCurrYear, "America/Denver", "d");
var month = Utilities.formatDate(prevDateCurrYear, "America/Denver", "M");
Logger.log("\nUnformatted prevDateCurrYear: "+prevDateCurrYear+
"\n\nYour original log:\nprevDateCurrYear.getMonth(): "+prevDateCurrYear.getMonth()+"\n"+
"prevDateCurrYear.getDay(): "+prevDateCurrYear.getDay()+"\n"+
"prevDateCurrYear.getYear(): "+prevDateCurrYear.getYear()+"\n"+
"\nUsing Utilites.formatDate():\nmonth: "+month+"\ndate: "+date+"\nyear: "+year+
"\nUtilities.formatDate(prevDateCurrYear, \"America/Denver\", \"M d yyyy\"): "
+Utilities.formatDate(prevDateCurrYear, "America/Denver", "M d yyyy"));
}
Log:
Unformatted prevDateCurrYear: Thu Feb 18 2016 11:29:42 GMT-0700 (MST)
Your original log:
prevDateCurrYear.getMonth(): 1
prevDateCurrYear.getDay(): 4
prevDateCurrYear.getYear(): 2016
Using 'Utilites.formatDate()`:
month: 2
date: 18
year: 2016
Utilities.formatDate(prevDateCurrYear, "America/Denver", "M d yyyy"): 2 18 2016