4

I'm having some problems getting a date string (such as "17/08/2012") to a date so that it can be used to compare another date.

I would like "17/08/2012" to produce the date "17/08/2012 00:00:00 GMT" so that it can be used correctly for comparison. I thought this would be the easiest part of what I'm trying to do, but apparently not. Please see my current code below.

function dateToString(dateString) {
  var dateArray = dateString.split("/");
  var year = dateArray[2];
  var month = dateArray[1];
  var day = dateArray[0];
  var date = new Date(year, month - 1, day);

  return date;
}

This code currently produces "Fri Aug 17 16:00:00 PDT 2012" and I have absolutely no idea why it says 4pm. I have been trying loads of different ways of doing this for the past hour and still cannot seem to get it right. Any ideas how I can get it to convert correctly?

As always any help is much appreciated.

4

1 回答 1

4

Your code is correct. 16 h PDT is the time in Pacific time zone, if I'm right that's 9 hours ahead of GMT. You should check the time zone settings of the script and of the spreadsheet so all operations on dates will be ok . Just keep in mind that the logger sometimes shows PDT value even when settings are good (at least it happened to me quite often in the past...) You can always choose how you display date objects by using Utilities.formatDate()

For more info on date object have a look at this reference page

于 2012-08-17T17:17:13.110 回答