3

I am trying to create a new date object initialized to a date passed in from another function. The program will not know the desired date in advance, but let's say the date is January 1, 2017.

Successful:

var newYear = new Date(2017, 0, 1);
Logger.log(newYear.toISOString());
//logs 2017-01-01T08:00:00.000Z

Unsuccessful:

var date = "2017, 0, 1";
var newYear = new Date(date);
Logger.log(newYear.toISOString());
//throws "Error, date range is invalid."

Unsuccessful:

var date = "2017-0-1";
var newYear = new Date(date);
Logger.log(newYear.toISOString());
//throws "Error, date range is invalid."

Edit: Just before posting I figured it out. I hope no one minds me posting and answering my own question-- maybe you can answer it better!

Dr.Queso
  • 761
  • 2
  • 6
  • 11
  • Note that using the Date constructor to parse strings is strongly recommended against. Always manually parse strings, a library can help but is usually not required as a 2 or 3 line function should suffice. – RobG Mar 08 '16 at 08:35
  • When you talk about manually parsing strings, do you mean like the solution I gave below, or am I misunderstanding you? – Dr.Queso Mar 10 '16 at 06:28
  • Yes. Note that for ISO 8601 date only fields, some browsers parse them as local, some as UTC (some change their mind from version to version) and some won't parse them at all, so manual is the only way. A library can help, but it can be parsed with validation in 3 lines (without obfuscation). – RobG Mar 10 '16 at 07:37
  • Related reading: http://stackoverflow.com/questions/2587345/javascript-date-parse – nikoskip Mar 10 '16 at 20:52

1 Answers1

2

Even though a string looks like a series of integers separated by commas, and even though Javascript is a loosely typed language, they are not the same.

Solution:

var year = 2017;
var month = 0; //don't forget months go from 0-11
var day = 1;
var newYear = new Date(year, month, day);
Logger.log(newYear.toISOString());
//logs 2017-01-01T08:00:00.000Z

To parse a date in the form of "2017-03-04":

var givenDate = "2017-3-04"
var date = givenDate.split("-");
var Year = date[0];
var Month = date[1]-1; 
var Day = date[2];
var newYear = new Date(Year, Month, Day);
Logger.log(newYear.toISOString()); 
//logs 2017-01-01T08:00:00.000Z

Note: to output date in your local time zone, instead of "toISOString()", which reports the time as GMT/ISO time (on most browsers), you can use "toDateString()" to output a more readable form which is reflective of your local time zone.

Dr.Queso
  • 761
  • 2
  • 6
  • 11
  • Note that while this parses the date as local (which is per ISO 8601), output using *toISOString* might show a different date since it's always UTC and the user might not be. For users east of Greenwich, the date will be one day earlier, e.g. for UTC+0500, 2017-01-01 will convert to 2017-01-01T00:00:00+0500 (local), which is 2016-12-31T19:00:00Z (i.e. *toISOString* shows an unexpected date because it's using a different time zone). – RobG Mar 10 '16 at 07:43