admin管理员组文章数量:1317914
Does the Date
object in Javascript ever use a non-Gregorian calendar?
The MDN and MSDN docs outline the methods on the Date
object and reference UTC and IETF-pliant RFC 2822 timestamps.
The Wikipedia article mentions
Days are conventionally identified using the Gregorian calendar, but Julian day numbers can also be used.
The MDN and MSDN documentation just says that the non-UTC methods refer to the "local time", but doesn't define what "local time" is.
I am working on interfacing to a webservice which is giving me back some data that includes a day-of-year field, which I need to pare to current day-of-year. I am well aware about the pitfalls of relying on an accurate time from a user's machine, and am fine with any problems that result from bad timezones and bogus date settings.
I am concerned, though, about users in locales that don't use the Gregorian calendar, and what their browsers will give back if I use the .getDate()
, .getMonth()
, and .getFullYear()
methods to pute day-of-year.
So, in practice, does "local time" in Javascript ever refer to a non-Gregorian calendar system, such as the Hebrew or Persian calendars?
Does the Date
object in Javascript ever use a non-Gregorian calendar?
The MDN and MSDN docs outline the methods on the Date
object and reference UTC and IETF-pliant RFC 2822 timestamps.
The Wikipedia article mentions
Days are conventionally identified using the Gregorian calendar, but Julian day numbers can also be used.
The MDN and MSDN documentation just says that the non-UTC methods refer to the "local time", but doesn't define what "local time" is.
I am working on interfacing to a webservice which is giving me back some data that includes a day-of-year field, which I need to pare to current day-of-year. I am well aware about the pitfalls of relying on an accurate time from a user's machine, and am fine with any problems that result from bad timezones and bogus date settings.
I am concerned, though, about users in locales that don't use the Gregorian calendar, and what their browsers will give back if I use the .getDate()
, .getMonth()
, and .getFullYear()
methods to pute day-of-year.
So, in practice, does "local time" in Javascript ever refer to a non-Gregorian calendar system, such as the Hebrew or Persian calendars?
Share Improve this question edited Oct 7, 2021 at 5:51 CommunityBot 11 silver badge asked May 23, 2012 at 15:54 mpdonadiompdonadio 2,9413 gold badges36 silver badges55 bronze badges 6-
I'm pretty sure that "local time" refers to the time zone offset from UTC. However the question is still interesting: can
getMonth()
ever return something bigger than 11, for instance. I strongly suspect the answer is "no" but I've got no evidence. – Pointy Commented May 23, 2012 at 16:02 - @Pointy, the ECMA-262 specs do say that local time == utc + time zone adjustment + daylight dayings time adjustment. However, we all know what the specs say and what browsers do aren't always the same thing. – mpdonadio Commented May 23, 2012 at 16:12
- 1 Julian days aren't a separate calendar system -- a Julian Day is simply the fractional number of days since noon on January 1, 4713 BC. Kind of like UNIX time, except with days, and with a much longer-ago epoch. en.wikipedia/wiki/Julian_day – user149341 Commented May 23, 2012 at 16:23
- 1 @duskwuff yes but puter people since like forever (believe me, I'm pretty old) have been mis-using the terminology - "Julian Date" usually means "day-of-year" – Pointy Commented May 23, 2012 at 16:33
- I edited the question. When I was writing it, I did get "Julian day" and "Julian calendar" mixed up in my head. Someone using the Hebrew, Persian, etc, calendars is what I am really contemplating. – mpdonadio Commented May 23, 2012 at 17:09
3 Answers
Reset to default 2I just tested by setting my puter (Mac OS X 10.7.4) to use the Hebrew calendar, and the behavior of the Date object was not affected in Safari, Firefox, or Chrome. Looks like it always uses the Gregorian calendar.
Re: Does the Date object in Javascript ever use a non-Gregorian calendar?
No. I don't see any backup for a claim that the Javascript Date object can use the Julian Calendar system.
However, you can convert a Javascript date object (a Gregorian date) into either a Julian Day or a Julian Date.
Julian Day
Julian day is used in the Julian date (JD) system of time measurement for scientific use by the astronomy munity, presenting the interval of time in days and fractions of a day since January 1, 4713 BC Greenwich noon. Julian date is remended for astronomical use by the International Astronomical Union.
Julian Date
The term Julian date is widely used to refer to the day-of-year (ordinal date) although this usage is not strictly in accord to standards set by some international organizations.
*Source for the above quotes is a wikipedia article, Julian_day
Note that neither format involves months since "Julian date" and "Julian day" are terms for a particular representation of a day in the Gregorian calendar. You can tell this by the fact that the Julian day definition uses a Gregorian date: "4713 BC Greenwich noon"
From the ECMAScript specs (3rd edition and 5th edition are nearly identical in this regard, but am quoting the 5th):
15.9.1.9 Local Time
Conversion from UTC to local time is defined by
LocalTime(t) = t + LocalTZA + DaylightSavingTA(t)
Conversion from local time to UTC is defined by
UTC(t) = t – LocalTZA – DaylightSavingTA(t – LocalTZA)
Note thatUTC(LocalTime(t))
is not necessarily always equal tot
.
LocalTZA
refers to local time zone adjustment, and DaylightSavingTA
is the adjustment for daylight savings.
I have not found any evidence of a browser (current or historic) that does anything different when a non-Gregorian calendar is enabled on a user's machine.
本文标签: javascriptDoes the Date object ever use a nonGregorian calendarStack Overflow
版权声明:本文标题:javascript - Does the Date object ever use a non-Gregorian calendar? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742024875a2415353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论