Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript creates new date minus one day

Actually I tried to set a jquery datepicker with a determinated date but when I create a new date for set the widget, javascript throw me one day minus

Code:

new Date('2016-04-14')

Result:

Wed Apr 13 2016 17:00:00 GMT-0700 (US Mountain Standard Time)

jsfiddle

like image 455
Mr. Newbie Avatar asked Mar 13 '23 15:03

Mr. Newbie


2 Answers

You should read the fine manual

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

You should probably go with new Date(2016, 3, 14) (3 because the month integer is zero-based). This will create a date in your timezone at midnight, April 14.

like image 55
Phil Avatar answered Mar 16 '23 07:03

Phil


To get the UTC(GMT) time use .toUTCString() instead of the default .toString().

If you want to create local time midnight date from string, use iso8601 like 2016-04-01T00:00:00-07:00. Note the timezone offset.

like image 36
Pavel Gatnar Avatar answered Mar 16 '23 06:03

Pavel Gatnar