Why does js subtract the day from a Date object with a specific format?

I get dates from a database in this format:

yyyy-mm-dd

When I create a javascript Date object using this line, it builds the day before the date.

You can check this on your console:

var d = new Date("2015-02-01"); 
d

You will receive January 31st! I tested many theories, but no one answered the question.

  • The day is not based on a zero value, otherwise it will give February 00, and not January 31.
  • He does not perform the mathematical equation by subtracting the day from the month and / or year
    • Date (2015-02-01) = Wed December 31, 1969
    • Date ("2015-01") = cf. December 31, 2014
  • It does not confuse day in a month
    • Date ("2015-08-02") = Sat Aug 01 2015
    • If so, the date will be February 08, 2015.
  • If you create the date in a different format, it works great
    • ( "02/01/2015" ) = 1 2015 .

, js . "", . js , ? , Date, ?

PS: " db" - , , db.

+4
2

UTC, - ,

, ,

.

Date UTC ,

, :

1. set UTC time:    

var D= new Date("2015-02-01"+'T00:00:00Z');


2. adjust for local:

D.setMinutes(D.getMinutes()+D.getTimezoneOffset());

D: ( ) Sun Feb 01 2015 00:00:00 GMT-0500 ( )

, .

Some differences between browsers when time zone is not specified in a parsed string:

(tested on Eastern Standard Time location)

(new Date("2015-02-01T00:00:00")).toUTCString();


Firefox 35: Sun, 01 Feb 2015 05:00:00 GMT

Chrome 40: Sun, 01 Feb 2015 00:00:00 GMT

Opera 27: Sun, 01 Feb 2015 00:00:00 GMT

IE 11: Sun, 01 Feb 2015 05:00:00 GMT

IE and Firefox set the Date as if it was local, Chrome and Opera as if it was UTC. 
+2

javascript Date 1 1970 00:00:00 UTC. , "" , Date " ", ( ).

Date parse. ( ISO 8601) .

, .

. 2015-02-01 1 2015 .

javascript, Date . Javascript , 1 1970 . , , (00:00:00), , UTC, .

javascript- :

var d = new Date("2015-02-01");
alert(d.getTime());

1422748800000

, , ( ) , getter, . . "" " " , UTC. , UTC, UTC, , .

0

All Articles