3

I am trying to create correct Date object

new Date("December 15, 2015")

I expect this

2015-12-15T00:00:00.000Z

But the actual outcome is this

2015-12-14T16:00:00.000Z

What is wrong?

kurumkan
  • 2,635
  • 3
  • 31
  • 55
  • Seems like timezone offset. – VLAZ Oct 29 '16 at 18:46
  • @vlaz I read about offsets. But I dont understand - why? Because I dont create Date based on current time right? So why it gives me date object with offset? – kurumkan Oct 29 '16 at 18:47
  • 1
    @ArslArsl Actually, I think this is probably going to come down to a text interpretation with current locale. It is assuming you want midnight, December 15, 2015, in your time zone. I don't know where in the spec that is, but that's what the issue will be. – Brad Oct 29 '16 at 18:48
  • 1
    I am not sure, I am just saying what I'm seeing. You wanted 00:00 on the 15th and you got 8 hours earlier than that. I'd guess you're in a -8 timezone, and the date is automatically formatted to that. – VLAZ Oct 29 '16 at 18:49
  • so tricky.. what can I do to avoid this kind confusing stuff? what modules are good for that? – kurumkan Oct 29 '16 at 18:51
  • Doesn't look like a RFC2822 compliant date ? – adeneo Oct 29 '16 at 18:54
  • `2015-12-14T16:00:00.000Z` is UTC, `2015-12-15` is your local time. Do you really want a UTC time or local time? – JJJ Oct 29 '16 at 19:14
  • 1
    @ArslArsl> Check out moment.js – Paul Oct 29 '16 at 19:22
  • @adeneo I think yes, you are right. But I want to treat correctly all "human readable" strings as parameter to my Date object. What is the best choise? – kurumkan Oct 29 '16 at 19:22
  • @JJJ I need utc. not local time – kurumkan Oct 29 '16 at 19:22
  • 1
    Possible duplicate of [create javascript date UTC](http://stackoverflow.com/questions/13364036/create-javascript-date-utc) – JJJ Oct 29 '16 at 19:26

1 Answers1

3

Run your node like this:

$ TZ=UTC node

and you don't have local time zone issues.

CFrei
  • 3,552
  • 1
  • 15
  • 29