3

This is so basic, but it makes no sense to me:

new Date("2010-01-01").getFullYear();

result: 2009

wth? My goal is to reformat the date as mm/dd/yyyy given the format yyyy-mm-dd..

Adding on:

new Date("2010-01-01").getMonth();

result: 11

new Date("2010-01-01").getDate();

result: 31

Patrick
  • 827
  • 3
  • 14
  • 20

3 Answers3

5

The date string you're passing into new Date() has no timezone in it. It's being interpreted as UTC. The critical thing to understand here is that a Date is stored as a Unix timestamp (seconds since 1970-01-01 00:00, making 'Date' a misleading name) so if you don't specify the time within the date, it's going to apply a default.

Date.prototype.getFullYear() retrieves the full year for that timestamp in your LOCAL time. (See the docs here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear)

You're somewhere west of UTC, and 2010-01-01 UTC is 2009-12-31 in your local time.

And for your final mystery....getMonth() is 0-based, not 1-based, so '11' is December.

S McCrohan
  • 6,663
  • 1
  • 30
  • 39
  • Please don't use "timestamp" to refer to the Date time value. A [*timestamp*](https://en.wikipedia.org/wiki/Timestamp) is any value that represents a date and perhaps time (so "2016-12-25" is a timestamp for some). ECMAScript Dates have a [*time value*](http://www.ecma-international.org/ecma-262/6.0/#sec-time-values-and-time-range). – RobG Mar 02 '16 at 04:28
  • For my sins, "timestamp" will always be "UNIX timestamp" to me, but you're right that that that isn't at all universal. Edited to be more precise. – S McCrohan Mar 02 '16 at 04:42
4

Don't use the Date constructor to parse strings, it's largely implementation dependent and inconsistent.

An ISO 8601 date and time without a timezone like "2016-02-29T12:00:00" should be treated as local (i.e. use the host system timezone offset to create a Date), but a date–only string is treated like "2016-02-29" as UTC. The first behaviour is consistent with ISO 8601, but the second isn't.

Some versions of browsers will treat date–only strings as UTC, and some as invalid dates, so always parse strings manually (a two line function or library can help). That way you know how it will be parsed in all hosts.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Oh thanks I'll do that. I'm new to js if that weren't apparent and I'm surprised there's not an out of the box date string parser...it seems wrong.. – Patrick Mar 02 '16 at 05:02
  • @Patrick—well, there is a parser but it's crap, it will be years before ISO 8601 can be used, and even then the only safe way will be with full date and time (with or without timezone). But in the meantime, parsing isn't difficult, just annoying. If you must deal with dates a lot, and have many different formats, consider a library. But usually they aren't necessary. – RobG Mar 02 '16 at 09:29
  • Yes, it's no big deal. I'm just surprised. – Patrick Mar 02 '16 at 12:18
-1

Provide Time with date in the new Date() as parameter . Then u will get exact Result.

Mohaimin Moin
  • 821
  • 11
  • 22