26

enter image description here

This is what I get in chrome console. I pass "2016-09-05"(YYYY-MM-DD) as the date and it shows me Sept 4,2016 as the date.

Another constructor shows the right date

enter image description here

Passing it comma separated needs some tokenizing + parsing + making month zero indexed which I want to avoid

Robin
  • 6,879
  • 7
  • 37
  • 35
  • Try "new Date("2016-05-09") " instead. – Kadaj Aug 30 '16 at 09:20
  • That gives me "Sun May 08 2016 20:00:00 GMT-0400 (EDT)" – Robin Aug 30 '16 at 09:22
  • 1
    No @Kadaj - the syntax for the `Date` constructor is correct: `new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]])` – Geoff James Aug 30 '16 at 09:22
  • 1
    The result seems correct. You are at UTC -4 – Gerardo Furtado Aug 30 '16 at 09:23
  • 1
    Hint: it's to do with the timezone that it's using - i.e. `GMT-0400` - so it's taking 4 hours from the `00:00` on that day (taking you to the previous one) - check the timezone your browser is using... – Geoff James Aug 30 '16 at 09:24
  • 1
    You *can* specify to use a time created from UTC - `new Date(Date.UTC(year, month, day, hour, minute, second))` (minute and second are optional) – Geoff James Aug 30 '16 at 09:25
  • @GeoffJames Just a complement: the values for `month` are 0, 1, 2 ... 11 – cezar Aug 30 '16 at 09:26
  • @cezar - I was wrong in last comment, so deleted it to prevent confusion (whoops, my bad). When you use the ctor as per my previous comment, yes - it does use `0-based` index. The ctor the OP is using is a string, so this will parse correctly. In this instance, if the OP were to use the ctor I've posted above - it would need to be `new Date(Date.UTC(2016,08,05));` or `new Date(2016,08,05);` – Geoff James Aug 30 '16 at 09:36
  • 1
    OK @Robin. Your post tags `datepicker`, but your question is referring to the console in Chrome (I'm not questioning the parsing of the date, by the way). Are you actually using a `datepicker`? If so, what is your HTML code? What are you trying to achieve? Since you've edited your post, you mention that you'd want to avoid using 0-based index to parse the date etc. Is this coming from a date-picker? You need to add the scope to which you're having the issue and your desired outcome. – Geoff James Aug 30 '16 at 09:45
  • Another question that might help you with the timezone offset issue: http://stackoverflow.com/questions/7403486/add-or-subtract-timezone-difference-to-javascript-date – Geoff James Aug 30 '16 at 09:48

3 Answers3

77

If you omit the time in the new Date(string) constructor, UTC time is assumed. So the displayed value is actually correct. Use new Date('2016-09-05T00:00') to create the date object in local time.

Edit: while some browsers seem to support the yyyy-MM-dd HH:mm format, it's not officially supported, and, as mentioned in the comments, this doesn't work in Safari. I've updated the answer to use a T instead of a space between date and time.

Per the ECMAScript® 2023 Language Specification:

Date-only forms:

  • YYYY
  • YYYY-MM
  • YYYY-MM-DD

It also includes “date-time” forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional UTC offset representation appended:

  • THH:mm
  • THH:mm:ss
  • THH:mm:ss.sss
Ruud
  • 1,262
  • 11
  • 14
1

You could use the Solution from UTC Date Conversion. Which basicall does the following:

console.log(new Date("2014-12-23"));
console.log(convertDateToUTC(new Date("2014-12-23")));

function convertDateToUTC(date) { 
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()); 
}

The output would be like that in the console (for me at least :D)

Tue Dec 23 2014 01:00:00 GMT+0100 (Mitteleuropäische Zeit)
Tue Dec 23 2014 00:00:00 GMT+0100 (Mitteleuropäische Zeit)

Community
  • 1
  • 1
djnose
  • 917
  • 7
  • 19
-1

use setFullYear the syntax is Date.setFullYear(year,month,day)

mydate = new Date();
mydate.setFullYear(2016, 08, 05);
konenas
  • 80
  • 1
  • 12