1

I created a function to generate an array of dates arr in 1-month increments beginning at 1/1/2013 and going until now.

function getDateRange() {
  var start = new Date('1/1/2013');
  var today = new Date();
  var arr = [start];
  var next = new Date(start);
  while (next < today) {
    arr.push(next);
    next = new Date(next.setMonth(next.getMonth() + 1));
  }
  Logger.log(arr);
  Logger.log(arr.map(formatDate));
}


function formatDate(d) {
 return Utilities.formatDate(d, 'MST', 'MMM-dd');
}


The function correctly generates arr, which looks like the following:

Jan 01 2013 00:00:00 GMT-0700 (MST),Fri Feb 01 2013 00:00:00 GMT-0700 (MST),Fri Mar 01 2013 00:00:00 GMT-0700 (MST),Mon Apr 01 2013 00:00:00 GMT-0600 (MDT),Wed May 01 2013 00:00:00 GMT-0600 (MDT)...

but then when I log arr.map(formatDate), I don't get the same dates starting at the 4th date:

Jan-01,Feb-01,Mar-01,Mar-31,Apr-30...

Any ideas why Utilities.formatDate is screwing up the dates?

  • `Jan 01 2013 00:00:00 GMT-0700 (MST)` should be `Jan-01` when formatted as `MMM-dd` – VLAZ Sep 13 '19 at 15:44
  • The first three dates look right, but it's after that that the dates start to get weird. – Jack Vawdrey Sep 13 '19 at 15:50
  • Ah that - it's daylight savings. You can see the timezone offset changes from March to April. SInce your date time is set to `00:00:00` and you format to the old timezone, you lose an hour and roll back into the previous month. – VLAZ Sep 13 '19 at 15:53
  • Stupid daylight savings. Good catch. Thanks @VLAZ – Jack Vawdrey Sep 13 '19 at 15:54
  • 2
    You should not depend on `new Date(string)` returning a correct date across browsers or even versions of the same browser, unless the versions are recent and the format of `string` is `yyyy-MM-dd` or `yyyy-MM-ddTHH:mm:ss`. See [Why does Date.parse give incorrect results?](https://stackoverflow.com/q/2587345/215552). – Heretic Monkey Sep 13 '19 at 15:56
  • @HereticMonkey Should I be worried about that if this script is always running within Google Apps Script? – Jack Vawdrey Sep 13 '19 at 16:01
  • Doesn't matter where it's running. If it's running a flavor of ECMAScript/JavaScript, it's got this issue. – Heretic Monkey Sep 13 '19 at 16:03
  • `next = new Date(next.setMonth(next.getMonth() + 1))` should be just `next.setMonth(next.getMonth() + 1)`, there is no need for the assignment or *new Date*. – RobG Sep 13 '19 at 20:32

1 Answers1

3

function getDateRange() {
  var start = new Date('1/1/2013');
  var today = new Date();
  var arr = [];
  do {
    arr.push(start);
    start = new Date(start.setDate(start.getDate() + 1));
  } while (start < today)
  console.log(arr);
  console.log(arr.map(formatDate));
}

function formatDate(date) {
  return date.toLocaleString("en-us", {
    month: "short",
    timeZone: 'UTC'
  }) + "-" + date.toLocaleString("en-us", {
    day: "numeric",
    timeZone: 'UTC'
  });
}
RobG
  • 142,382
  • 31
  • 172
  • 209
ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
  • 1
    Thanks @Ankit. I was able to resolve the issue by setting the initial datetime to be at 12:00 so that it wouldn't be effected by daylight savings time. – Jack Vawdrey Sep 13 '19 at 17:51
  • Thats great, Yeah that can be one more option to resolve this issue. Thank you! – ankitkanojia Sep 13 '19 at 18:03