7

The given date is 2016-02-04. That's suppose to be Feb. 4, 2016. But when I use new Date(), it returns as Wed Feb 03 2016 16:00:00 GMT-0800 (PST) and not Thu Feb 04....

Below is all I literally do:

var _entryDate = new Date("2016-02-04");
console.log(_entryDate); // Wed Feb 03 2016 16:00:00 GMT-0800 (PST)

Why is this happening and how do I get my desired result which is Feb. 4 and not the day before that?

junerockwell
  • 838
  • 1
  • 9
  • 29

4 Answers4

6

The format you're using is interpreted as being a UTC date, so the time is assumed to be midnight in Western Europe. That's 8 hours ahead of you.

You can force the time zone to be interpreted by tacking on T00:00-0800 to the date string. It might be more robust for you to parse the date yourself and construct your Date instance with numeric year, month, and date parameters.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • So is it like Date constructor will fall back to `UTC` if we miss `tz` part in the string passed to constructor? I thought it will fall back to local 'tz' :) – sabithpocker Feb 04 '16 at 16:11
  • 1
    @sabithpocker for ISO date/time strings the default is `Z` (UTC). If you create a date with numeric year, month, and day, the default is the local time zone. – Pointy Feb 04 '16 at 16:42
5

var dateArray = "2016-02-04".split("-");
var year = dateArray[0];
var month = parseInt(dateArray[1], 10) - 1;
var date = dateArray[2];
var _entryDate = new Date(year, month, date);
alert(_entryDate);

Removed unnecessary parseInt for month and year as suggested by @RobG

And I have no idea on what Pointy points to(as in ignorant). I never knew date constructer will fall back to UTC depending on format of String passed. This way Date constructor will build Date using local timezone, always. Anyway its always good to use Date constructer with numeric parameters.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75
  • Good idea to manually parse the string, but there is no need for *parseInt* here. Arguments passed to the Date constructor [*are first converted to Number*](http://www.ecma-international.org/ecma-262/6.0/#sec-date-year-month-date-hours-minutes-seconds-ms). – RobG Feb 04 '16 at 23:34
  • @RobG Thank you for the piece of advice. I never knew that. – sabithpocker Feb 05 '16 at 04:49
2

Javascript Date objects include a time as well. If you create one with no time component, it defaults to midnight.

The question is, midnight in what time zone? If you are parsing a date from a string, that is not specified in the ECMAScript definition; it's up to the implementation. But most implementations choose Universal Time - which means that the represented instant falls on the previous day anywhere west of London and east of the international date line.

If you create the date from numbers instead of parsing a string, it is specified to be local time instead, in whatever time zone Javascript thinks it's in. For example, here in US/Eastern ("EST"):

new Date('2016-02-04')
//=> Wed Feb 03 2016 19:00:00 GMT-0500 (EST)  - midnight UTC
new Date(2016,2,4)
//=> Fri Mar 04 2016 00:00:00 GMT-0500 (EST)  - midnight EST

You can add an explicit time and time zone to the end of the string:

new Date('2016-02-04T00:00:00-05:00')

but you have to know the time zone offset to do that. Or you can manually parse the string and use the numeric constructor, which is probably your safest bet.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
0

It's because date is stored (and parsed) in UTC, but displayed in your local time zone. To display correct date, you can use

new Date("2016-02-04").toISOString()
berliner
  • 1,887
  • 3
  • 15
  • 23