如何在 Javascript 中将波斯 (Jalali) 日期转换为其他 18 个日历日期,无需外部库或复杂的天文方程
TL;DR, 要求是能够采用波斯 (Jalali) 日期(也称为波斯阳历回历),例如 Esfand 19, 1400
(即 “12/19/1400”
并将其转换为其他日历(公历、伊斯兰教、中国、希伯来语等)无需使用外部库或复杂的天文方程,并且无需使用新日期 Temporal API 到 Javascript 中的待实现。
Javascript 内置方法 Intl.DateTimeFormat()
将公历日期转换为各种日历日期(18 个世界日历),包括输出字符串的格式,
但是从今天(三月)开始 。 2022),Javascript 不提供反向操作的内置方法,即将波斯日期(和其他日历的日期)转换回公历日期或其他日历,为此,您将需要使用外部日期库。进行诸如“moment.js”等的转换。
我进行日期转换的方法如下,作为 StackOverflow 推荐的此问题的答案:我可以回答我自己的问题吗?
TL;DR, The requirement is to be able to take a Persian (Jalali) date (also known as Persian Solar Hijri Calendar) like Esfand 19, 1400
(i.e. "12/19/1400"
and convert it to other calendars (Gregorian, Islamic, Chinese, Hebrew, etc.) without using external libraries or complex Astronomical Equations. And without using the pending implementation of the new date Temporal API into Javascript.
Javascript built-in method Intl.DateTimeFormat()
converts Gregorian Dates into various calendars' dates (18 World Calendars) including the formatting of the output string.
However, as of today (March 2022), Javascript does not provide a built-in method for the reverse operation, i.e. converting the Persian Dates (and other calendars' dates) back into Gregorian Dates or into other calendars. For such purposes, you will need to use External Date Libraries to do the conversion such as 'moment.js' and many others.
My method of doing the date conversion follows as an answer to this question as recommended by StackOverflow here: Can I answer my own question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的简短 Javascript 函数不使用外部库,并提供将波斯 (Jalali) 日期(从波斯年 -272,442 AP 到 +275,139 AP)转换为任何日期的工具以下 18 个 Javascript 日历,带有用于格式化结果输出的选项:
"buddhist", "chinese", "coptic", "dangi", “ethioaa”、“埃塞俄比亚”、“格雷戈里”、“希伯来语”、“印度”、“伊斯兰”、“伊斯兰-umalqura”、“伊斯兰-tbla”、“伊斯兰-民用”、“伊斯兰-rgsa”、“iso8601” "、"japanese"、"persian"、"roc"、"islamicc"。
该方法也不使用复杂的数学或天文公式,并且仅依赖于 Javascript 内置日历转换算法,而这些算法又基于 ICU 代码 [https://icu.unicode.org/]。
这种方法可确保输出始终准确且完全符合 Javascript 引擎输出。
语法
在最简单的形式中,该函数默认使用 ISO 日期格式将波斯日期转换为
公历
日历。示例:将波斯日期 Esfand 19, 1400(即 12/19/1400)转换为公历。
将波斯日期转换为另一个日历(例如“伊斯兰”日历):
向输出添加格式设置,使用 Javascript
Intl.DateTimeFormat()
方法中的'dateStyle'
选项。示例:使用完整的 dateStyle 将波斯日期转换为伊斯兰日期
示例:使用波斯区域设置将波斯日期转换为希伯来语
上述操作可用于所有其他 18 个日历。
一项附加功能是能够将波斯日期格式化为任何可用的
'dateStyles'
和'locales'
无需转换。为此,请将
'toCal'
指定为persian
示例:使用波斯语言区域设置格式化波斯日期
示例:格式化波斯日期在印地语区域设置中
您可以使用
Intl.DateTimeFormat()
中的所有可用选项来格式化输出日期。无效的波斯日期
如果将无效的波斯日期传递给函数,将生成错误
无效的波斯日期!
。无效的波斯日期是指当月中的日期不正确或者日期或月份不正确的日期。
例如,波斯日期 1400/12/30 无效,因为波斯历的 12 月(“Esfand”月)是 1400 年的 29 天。
未来的 Javascript Temporal API 将使此任务变得更简单。
The short Javascript function below does not use external libraries and provides the facilities to convert a Persian (Jalali) Dates (from Persian year -272,442 AP to +275,139 AP) into any of the following 18 Javascript Calendars with options for formatting the resulting output:
"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc".
The method, also, does not use complex mathematical or astronomical formulas and relies solely on the Javascript built-in calendar conversion algorithms which are in turn based on the ICU code [https://icu.unicode.org/].
This approach ensures that the output is always accurate and fully compliant with the Javascript engine output.
Syntax
In its simplest form, the function defaults to converting the Persian Date into the
Gregorian
calendar using the ISO Date Format.Example: Convert the Persian Date Esfand 19, 1400 (i.e. 12/19/1400) to Gregorian.
To to convert Persian Date to another calendar (say 'Islamic' calendar):
To add formatting to the output, use the
'dateStyle'
options as in the JavascriptIntl.DateTimeFormat()
method.Example: Convert Persian Date to Islamic Date with full dateStyle
Example: Convert a Persian Date into Hebrew with Persian Locale
The above can be done for all other 18 Calendars.
An added feature is the ability to format the Persian Date into any of the available
'dateStyles'
and'locales'
without conversion.To do that specify the
'toCal'
topersian
Example: Use Persian Locale to Format a Persian Date
Example: Format a Persian Date in the Hindi Locale
You can use all the options available in the
Intl.DateTimeFormat()
for formatting the output date.Invalid Persian Dates
If an invalid Persian Date is passed to the function an error
Invalid Persian Date!
will be generated.Invalid Persian Dates are dates that have incorrect days in the month or incorrect days or months.
For example, the Persian Date 1400/12/30 is invalid because month 12 of the Persian Calendar (month "Esfand") is 29 days in the year 1400.
The future Javascript Temporal API will make this task simpler.
只需一行代码。
有关更多信息,请查看MDN 文档
Just one line of code.
for more information check MDN doc