javascript Date timezone issue -


i need js date object specified values date , year. expect new date("2000-01-01") give me date object 2000 value getfullyear(), if computer's time settings set chicago timezone, i'm getting fri dec 31 1999 18:00:00 gmt-0600 (cst), , buenos aires: fri dec 31 1999 22:00:00 gmt-0200 (arst).

is there way create date object, .getfullyear() returning date set in constructor, no matter timezone set on user's machine?

update: need date object used in library (which calls .getfullyear() method, using utc getters doesn't help.

when parsing string date in javascript, value in yyyy-mm-dd format interpreted utc value, rather local-time value.

the key parts separated hyphens, , there no time zone information in string. ecmascript 5.1 spec says in §15.9.1.15:

... value of absent time zone offset “z”.

that means, if don't specify offset, assume meant utc.

note since opposite of iso-8601 says, behavior has been changed in ecmascript 2015 (6.0), says in §20.3.1.16:

... if time zone offset absent, date-time interpreted local time.

therefore, when provision of es6 implemented properly, string values of format used interpreted utc interpreted local time instead. i've blogged here.

the workaround simple. replace hyphens slashes:

var s = "2000-01-01"; var dt = new date(s.replace(/-/g, '/')); 

another workaround acceptable assign time of noon instead of midnight date. parsed local time, , far enough away avoid dst conflicts.

var s = "2000-01-01"; var dt = new date(s + "t12:00:00"); 

alternatively, consider library moment.js more sensible.

var s = "2000-01-01"; var dt = moment(s, 'yyyy-mm-dd').todate(); 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -