admin管理员组文章数量:1290099
In javascript, if I specify the date as MM/DD/YYYY
, I can use new Date()
to parse it as the local timezone:
>>> new Date('01/01/1970')
Date {Thu Jan 01 1970 00:00:00 GMT-0500 (EST)}
However, if I specify the date as YYYY-MM-DD
, it assumes that I'm giving the date in the UTC timezone:
>>> new Date('1970-01-01')
Date {Wed Dec 31 1969 19:00:00 GMT-0500 (EST)}
Is there an easy way to tell the date parser to use the local timezone when parsing 'YYYY-MM-DD' dates? Or do I need to use .replace(/^(\d{4})-(\d{2})-(\d{2})$/, '$2/$3/$1')
to fix it first?
In javascript, if I specify the date as MM/DD/YYYY
, I can use new Date()
to parse it as the local timezone:
>>> new Date('01/01/1970')
Date {Thu Jan 01 1970 00:00:00 GMT-0500 (EST)}
However, if I specify the date as YYYY-MM-DD
, it assumes that I'm giving the date in the UTC timezone:
>>> new Date('1970-01-01')
Date {Wed Dec 31 1969 19:00:00 GMT-0500 (EST)}
Is there an easy way to tell the date parser to use the local timezone when parsing 'YYYY-MM-DD' dates? Or do I need to use .replace(/^(\d{4})-(\d{2})-(\d{2})$/, '$2/$3/$1')
to fix it first?
- According to developer.mozilla/en-US/docs/Web/JavaScript/Reference/…, "If you do not specify a time zone, the local time zone is assumed." I'm not sure why this is not the case. – mash Commented Jul 27, 2013 at 3:21
-
2
or
new Date('1970-01-01'.replace(/-/g,'/'))
and it will parsed as localtime. – fbynite Commented Jul 27, 2013 at 5:04 -
1
I think this thread can give you a lot of insights about
Data.parse
:) – Mike Li Commented Jul 27, 2013 at 5:41 - Great question. This issue drove me crazy for hours. – Josh Buchea Commented Jul 21, 2015 at 22:43
2 Answers
Reset to default 5The function bellow will do exactly what you want, so you can pass a string in the format "YYYY-MM-DD" and it will return a Date object in your local time zone.
function yyyymmddToLocalDate(isoString) {
const [year, month, day] = isoString.split('-');
return new Date(year, month - 1, day);
}
If you're in Brazil for example and call it yyyymmddToLocalDate('2020-10-07')
it will return Wed Oct 07 2020 00:00:00 GMT-0300 (Brasilia Standard Time)
Date.parse behaves as follows:
http://www.ecma-international/ecma-262/5.1/#sec-15.9.4.2
The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
http://www.ecma-international/ecma-262/5.1/#sec-15.9.1.15
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mm
The value of an absent time zone offset is “Z”.
So in the first case, since it doesn't fit the Date Time String Format, the implementation-specific parse takes effect (which happens to be based on local time). In the second case, it does fit the DTSF, so it is parsed as if the time zone was unspecified (which is supposed to be UTC), hence the disparity
本文标签: javascriptParse YYYYMMDD dates using the local timezoneStack Overflow
版权声明:本文标题:javascript - Parse YYYY-MM-DD dates using the local timezone - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741491671a2381647.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论