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

Travis Heeter

I get dates from the database in this format:

yyyy-mm-dd

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

You can test this in your console:

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

You will get January 31st! I've tested many theories, but none answer the question.

  • The day is not zero-based, otherwise it would give Feb 00, not Jan 31
  • It's not performing a math equation, subtracting the day from the month and/or year
    • Date(2015-02-01) = Wed Dec 31 1969
    • Date("2015-01") = Wed Dec 31 2014
  • It is not confusing the day for the month
    • Date("2015-08-02") = Sat Aug 01 2015
    • If this were true the date would be Feb 08 2015
  • If you create a Date using a different format, it works fine
    • Date("02/01/2015") = Feb 1st, 2015

My conclusion is that js does this purposefully. I have tried researching 'why' but can't find an explanation. Why does js build dates this way, but only with this format? Is there a way around it, or do I have to build the Date, then set it to the next day?

PS: "How to change the format of the date from the db" is not what I'm asking, and that is why I'm not putting any db info here.

kennebec

Some browsers parse a partial date string as UTC and some as a local time,

so when you read it the localized time may differ from one browser to another

by the time zone offset.

You can force the Date to be UTC and add the local offset if you

want the time to be guaranteed local:

1. set UTC time:    

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


2. adjust for local:

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

value of D: (local Date) Sun Feb 01 2015 00:00:00 GMT-0500 (Eastern Standard Time)

Offset will be whatever is local time.

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. 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Subtract one day from date with format as mm/dd/yyyy

From Java

How to subtract a day from a date?

From Dev

How to subtract a day from datetime format in python?

From Dev

Powershell Subtract 1 day from variable date

From Dev

Subtract 1 day from date in different timezone using moment.js and moment-timezone.js

From Dev

Subtract 1 day from date in different timezone using moment.js and moment-timezone.js

From Dev

Subtract one day from DateTime object

From Dev

Subtract day in specific format

From Dev

date_create_from_format php on certain month and date does not return correct date

From Dev

subtract time from date - moment js

From Dev

from Date I just want to subtract 1 day in javascript/angularjs

From Dev

SQL: Subtract 1 day from date value of my sql query

From Dev

Why when I access an object consisting of three integers, does it subtract from the base pointer, and not the stack pointer?

From Dev

Angular.js format date from json object

From Java

Format date and Subtract days using Moment.js

From Java

Day Name from Date in JS

From Dev

Subtract days from date

From Dev

Convert date from weird string to date in format day month and year

From Dev

Moment JS - how to subtract 7 days from current date?

From Dev

Create Date object in PHP for dates before 1970 in certain format

From Dev

Create Date object in PHP for dates before 1970 in certain format

From Dev

why does gcc subtract 1 from an expression while assigning the expression?

From Dev

r - how to subtract first date entry from last date entry in grouped data and control output format

From Dev

JavaScript / Underscore.js - dynamically subtract values from nested object

From Dev

BATCH: Subtract the system date with one day before

From Dev

Plot XLabel date format from object 'dateIndex'

From Dev

Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

From Dev

Why does date() not convert YYMMDDHHMM to MySQL acceptable date format correctly?

From Dev

how to get day format from new Date in Angularjs

Related Related

  1. 1

    Subtract one day from date with format as mm/dd/yyyy

  2. 2

    How to subtract a day from a date?

  3. 3

    How to subtract a day from datetime format in python?

  4. 4

    Powershell Subtract 1 day from variable date

  5. 5

    Subtract 1 day from date in different timezone using moment.js and moment-timezone.js

  6. 6

    Subtract 1 day from date in different timezone using moment.js and moment-timezone.js

  7. 7

    Subtract one day from DateTime object

  8. 8

    Subtract day in specific format

  9. 9

    date_create_from_format php on certain month and date does not return correct date

  10. 10

    subtract time from date - moment js

  11. 11

    from Date I just want to subtract 1 day in javascript/angularjs

  12. 12

    SQL: Subtract 1 day from date value of my sql query

  13. 13

    Why when I access an object consisting of three integers, does it subtract from the base pointer, and not the stack pointer?

  14. 14

    Angular.js format date from json object

  15. 15

    Format date and Subtract days using Moment.js

  16. 16

    Day Name from Date in JS

  17. 17

    Subtract days from date

  18. 18

    Convert date from weird string to date in format day month and year

  19. 19

    Moment JS - how to subtract 7 days from current date?

  20. 20

    Create Date object in PHP for dates before 1970 in certain format

  21. 21

    Create Date object in PHP for dates before 1970 in certain format

  22. 22

    why does gcc subtract 1 from an expression while assigning the expression?

  23. 23

    r - how to subtract first date entry from last date entry in grouped data and control output format

  24. 24

    JavaScript / Underscore.js - dynamically subtract values from nested object

  25. 25

    BATCH: Subtract the system date with one day before

  26. 26

    Plot XLabel date format from object 'dateIndex'

  27. 27

    Converting date and time from String format to Python datetime object: ValueError: time data '... p.m.' does not match format '... %p'

  28. 28

    Why does date() not convert YYMMDDHHMM to MySQL acceptable date format correctly?

  29. 29

    how to get day format from new Date in Angularjs

HotTag

Archive