1

If I try to pull first day number from November, this works fine:

var d = new Date(2013, 10, 1);    // 1st of November 2013
d.toISOString();                  // 2013-11-01T00:00:00.000Z (November)
d.getDay();                      // 5 (Correct, 1st of November = Friday = 5)

However due to daylight savings time in the UK (the clocks going forward by one hour at the end of October), if I try to pull the first day of October, the date ends up being set to 23:00 on the previous day:

var d = new Date(2013, 9, 1);     // 1st of October 2013
d.toISOString();                  // 2013-09-30T23:00:00.000Z (September)
d.getDay();                      // 2 (Last day number of September)

How can I handle this so that I always get 00:00:00 on the 1st of the month, regardless of daylight savings time (and any other clock adjustments wherever a user may be in the world)?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 1
    for d.getDate(); on the second one, I get 1 when I try it myself – Se Won Jang Oct 09 '13 at 09:05
  • and I do get 1st of October for d.toISOString() as well – Se Won Jang Oct 09 '13 at 09:06
  • This does appear to be a daylight savings time issue in the UK. I've modified my question (somewhat significantly). – James Donnelly Oct 09 '13 at 09:09
  • It looks like the problem is that `toISOString()` returns the UTC time, so you will get inconsistent results all over the world. [I suggest checking this question out](http://stackoverflow.com/questions/17415579/how-to-iso-8601-format-a-date-with-timezone-offset-in-javascript) – musefan Oct 09 '13 at 09:20
  • 2
    You're confusing `getDate` with `getDay`? – Bergi Oct 09 '13 at 09:21

3 Answers3

5

You are looking for the getDay method, not for getDate. Both methods do always work with the local time, regardless of timezone and DST.

However, .toISOString does not. It formats the time in UTC, and leads to your confusion. Use the .toString function instead which also outputs the local time.

> new Date(2013, 9, 1).toISOString()
"2013-09-30T22:00:00.000Z" // my timezone's midnight in UTC
> new Date(2013, 9, 1).toString()
"Tue Oct 01 2013 00:00:00 GMT+0200" // midnight, with offset
> new Date(2013, 9, 1).getDate()
1 // the *first* day of October
> new Date(2013, 9, 1).getDay()
2 // is a Tuesday
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

Use new Date(Date.UTC(year, month, day [, hour, minute, second, millisecond])) to create a Date object from a specific UTC time.

var d = new Date(Date.UTC(2013, 9, 1));
d.toISOstring(); //'2013-10-01T00:00:00.000Z'
d.toString(); //'Fri Nov 01 2013 02:00:00 GMT+0200 (EET)'
mutil
  • 3,205
  • 1
  • 27
  • 34
0

In that case, try to set it to October 1st 1am or something: (depending on how much you swithc the time)

===== edit 1 ======

does javascript .setDate(1) function behave properly? I'm sorry I can't test your case in my timezone I think

Se Won Jang
  • 773
  • 1
  • 5
  • 12
  • I don't think this really helps solve the problem, as you would have to have the code check to see if it was BST or not, and they are not fixed dates each year – musefan Oct 09 '13 at 09:13
  • 1
    The problem with this approach is that this would be very specific to the UK. I'm wanting to be able to pull the first day of any month regardless where in the world a user is located. – James Donnelly Oct 09 '13 at 09:14