- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
TL;DR, 要求能够采用波斯(Jalali)日期(也称为波斯太阳回历),如 Esfand 19, 1400
(即 "12/19/1400"
)并将其转换为其他日历(公历、伊斯兰、中国、希伯来等) .) 不使用外部库或复杂的天文方程 。并且不使用新日期 Temporal API 到 Javascript 中的未决实现。
Javascript 内置方法 Intl.DateTimeFormat()
将公历日期转换为各种日历的日期(18 个世界日历),包括输出字符串的格式。
但是,截至今天(2022 年 3 月),Javascript 没有提供用于反向操作的内置方法,即将波斯日期(和其他日历的日期)转换回公历日期或其他日历。为此,您将需要使用外部日期库进行转换,例如“moment.js”等。
我的日期转换方法如下作为 StackOverflow 推荐的此问题的答案:Can I answer my own question?
最佳答案
下面的简短 Javascript 函数不 使用外部库并提供工具将波斯 (Jalali) 日期( 从波斯年 -272,442 AP 到 +275,139 AP )转换为以下 18 个 Javascript 日历中的任何一个,并带有用于格式化结果输出的选项:"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc", "islamicc".
该方法也不使用复杂的数学或天文公式,仅依赖于 Javascript 内置日历转换算法,而这些算法又基于 ICU 代码 [https://icu.unicode.org/]。
这种方法可确保输出始终准确并完全符合 Javascript 引擎输出。
句法
persianToCalendars(year, month, day, [options])
在其最简单的形式中,该函数默认将波斯日期转换为
Gregorian
使用 ISO 日期格式的日历。
persianToCalendars(1400,12,19);
output: 2022-03-10T00:00:00.000Z // default output Gregorian ISO format
要将波斯日期转换为另一个日历(例如“伊斯兰”日历):
persianToCalendars(1400,12,19, { toCal: "islamic-umalqura" });
output: 8/7/1443 AH
要为输出添加格式,请使用
'dateStyle'
Javascript 中的选项
Intl.DateTimeFormat()
方法。
persianToCalendars(1400,12,19, { toCal: "islamic-umalqura", dateStyle: "full" });
output: Thursday, Shaʻban 7, 1443 AH
示例:将波斯语日期转换为带有波斯语语言环境的希伯来语
persianToCalendars(1400,12,19, { toCal:"hebrew", dateStyle: "full", locale:"fa"})
output: پنجشنبه ۷ واذار الثانی ۵۷۸۲ تقویم عبری
以上可以为所有其他 18 个日历完成。
'dateStyles'
和
'locales'
无转换 .
'toCal'
至
persian
示例:使用波斯语区域设置格式化波斯日期
persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"fa"}));
output: ۱۴۰۰ اسفند ۱۹, پنجشنبه // mind the RTL requirements
示例:在印地语语言环境中格式化波斯日期
persianToCalendars(1400,12,19,{ toCal : "persian", dateStyle : "full", locale : "hi"}));
output: AP गुरुवार, 19 ईस्फन्द् 1400
您可以使用
Intl.DateTimeFormat()
中提供的所有选项。用于格式化输出日期。
Invalid Persian Date!
会生成。
/*********************************************************************
* @function : persianToCalendars(year, month, day, [options])
*
* @purpose : Converts Persian/Iranian Date (Jalali Date) to the corresponding Gregorian Date.
* Handles Persian dates from -272,442 AP to +275,139 AP.
* Uses the 'JS Calendar Conversion by Target Approximation' Method.
* No external libraries or complex mathematical/astronautical formulas.
*
* @version : 1.00
* @author : Mohsen Alyafei
* @date : 17 Feb 2022
* @licence : MIT
* @param : year : (numeric) Persian year (-272442 to 275139)
* @param : month : (numeric) Persian month (1 to 12) note: months is standard 1 based
* @param : day : (numeric) Persian day (1 to 31)
* @param : options: Object with the following optional parameters:
*
* 'toCal' : Specifies the the type of output Calendar to convert to with 18 Calendars:
* - "gregory" : (default)
* - "buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic",
* "hebrew", "indian", "islamic", "islamic-umalqura", "islamic-tbla",
* "islamic-civil", "islamic-rgsa", "iso8601", "japanese", "persian", "roc".
*
* 'dateStyle' Same as used in the Intl.DateTimeFormat() constructor.
* If not stated, default output is in Gregorian ISO Format: YYYY:MM:DDTHH:mm:ss.sssZ
*
* 'locale' The BCP 47 language tag for formatting (default is 'en'). If the 'locale'
* is given then no date conversion happens and the Persian date is formatted
* based on the specified 'dateStyle' and 'locale'.
*
* Other options: As used in the Intl.DateTimeFormat() constructor.
*
* @returns : Return the date in the calendar and format of the specified 'options'
**********************************************************************/
//==========================================================
function persianToCalendars(year, month, day, op={}) {
const formatOut= gD=> "toCal"in op?(op.calendar=op.toCal,new Intl.DateTimeFormat(op.locale??"en",op).format(gD)):gD,
dFormat = new Intl.DateTimeFormat('en-u-ca-persian',{dateStyle:'short',timeZone:"UTC"});
let gD = new Date(Date.UTC(2000,month,day));
gD = new Date(gD.setUTCDate(gD.getUTCDate() + 226867));
const gY = gD.getUTCFullYear()-2000+year;
gD = new Date(((gY<0)?"-":"+")+("00000"+Math.abs(gY)).slice(-6)+"-"+("0"+(gD.getUTCMonth()+1)).slice(-2)+"-"+("0"+(gD.getUTCDate())).slice(-2));
let [pM,pD,pY] = [...dFormat.format(gD).split("/")], i=0;
gD = new Date(gD.setUTCDate(gD.getUTCDate() +
~~(year*365.25+month*30.44+day-(pY.split(" ")[0]*365.25+pM*30.44+pD*1))-2));
while (i < 4) {
[pM,pD,pY]=[...dFormat.format(gD).split("/")];
if (pD==day && pM==month && pY.split(" ")[0]==year) return formatOut(gD);
gD = new Date(gD.setUTCDate(gD.getUTCDate()+1));i++;
}
throw new Error('Invalid Persian Date!');
}
//==========================================================
//==========================================================
// Test Units
//==========================================================
console.log("-".repeat(55));
console.log("Convert the Persian Date '1400-12-19' to other calendars:");
console.log("input to function: persianToCalendars(1400,12,19, options)");
console.log("-".repeat(55));
console.log("Default (Gregory) ISO format : ",persianToCalendars(1400,12,19)); // convert to default gregorian date
console.log("Gregory 'full' format : ",persianToCalendars(1400,12,19,{toCal:"gregory",dateStyle:"full"}));
console.log("Islamic 'full' format : ",persianToCalendars(1400,12,19,{toCal:"islamic",dateStyle:"full"}));
console.log("Islamic-Umaalqura 'short'format: ",persianToCalendars(1400,12,19,{toCal:"islamic-umalqura"}));
console.log("Islamic-Umaalqura 'full' format: ",persianToCalendars(1400,12,19,{toCal:"islamic-umalqura",dateStyle:"full"}));
console.log("Islamic-civil 'full' format : ",persianToCalendars(1400,12,19,{toCal:"islamic-civil",dateStyle:"full"}));
console.log("Islamic-tbla 'full' format : ",persianToCalendars(1400,12,19,{toCal:"islamic-tbla",dateStyle:"full"}));
console.log("Islamic-rgsa 'full' format : ",persianToCalendars(1400,12,19,{toCal:"islamic-rgsa",dateStyle:"full"}));
console.log("Hebrew 'full' format : ",persianToCalendars(1400,12,19,{toCal:"hebrew",dateStyle:"full"}));
console.log("Indian 'full' format : ",persianToCalendars(1400,12,19,{toCal:"indian",dateStyle:"full"}));
console.log("Buddhist 'full' format : ",persianToCalendars(1400,12,19,{toCal:"buddhist",dateStyle:"full"}));
console.log("Chinese 'full' format : ",persianToCalendars(1400,12,19,{toCal:"chinese",dateStyle:"full"}));
console.log("Dangi (Korean) 'full' format : ",persianToCalendars(1400,12,19,{toCal:"dangi",dateStyle:"full"}));
console.log("R.O.C. (Minguo) 'full' format : ",persianToCalendars(1400,12,19,{toCal:"roc",dateStyle:"full"}));
console.log("Japanese 'full' format : ",persianToCalendars(1400,12,19,{toCal:"japanese",dateStyle:"full"}));
console.log("Coptic 'full' format : ",persianToCalendars(1400,12,19,{toCal:"coptic",dateStyle:"full"}));
console.log("Ethioaa 'full' format : ",persianToCalendars(1400,12,19,{toCal:"ethioaa",dateStyle:"full"}));
console.log("Ethiopic 'full' format : ",persianToCalendars(1400,12,19,{toCal:"ethiopic",dateStyle:"full"}));
console.log("-".repeat(55));
console.log("Format the input Persian Date without conversion:");
console.log("-".repeat(55));
console.log("Persian 'full' format : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full"}));
console.log("Persian 'medium' format : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"medium"}));
console.log("Persian 'short' format : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"short"}));
console.log("Persian 'ar' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"ar"}));
console.log("Persian 'fa' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"fa"}));
console.log("Persian 'hi' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"hi"}));
console.log("Persian 'ur' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"ur"}));
console.log("Persian 'ps-AF' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"ps-AF"}));
console.log("Persian 'id' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"id"}));
console.log("Persian 'pa' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"pa"}));
console.log("Persian 'ma' locale : ",persianToCalendars(1400,12,19,{toCal:"persian",dateStyle:"full", locale:"ma"}));
console.log("-".repeat(55));
console.log("Convert Max Negative and Max Positive Persian Dates to Gregorian");
console.log("-".repeat(55));
console.log(persianToCalendars(-272442,12,29)); // max negative Persian date
console.log(persianToCalendars(275139,6,23)); // max positive Persian date
console.log("-".repeat(55));
关于javascript - 如何在没有外部库或复杂天文方程的 Javascript 中将波斯 (Jalali) 日期转换为其他 18 个日历日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71421825/
在我的一门类(class)中,我接到了一项家庭作业,要求我们在谷歌上搜索 Metapost 语言并找到该语言中方程求解功能的用途。 在浏览了 Metapost 用户手册的前十多页后,我发现只有一个原因
你能帮我在 this page 上的谷歌图表上隐藏趋势线上的工具提示(方程)吗? ? 谢谢 以下是我正在使用的图表选项: var options = { title: 'Weight
我正在尝试将 TeXWorks 编辑器配置为使用与 TeXMaker 相同的语法着色。但是,TexWorks 使用正则表达式来指定应该着色的内容。不幸的是,它没有数学的默认设置。 我想匹配 $ 之间的
我刚开始玩 GHCi。我看到列表生成器基本上解决了给定集合中的方程式: Prelude> [x | x [0.01,0.2..2.0] [1.0e-2,0.2,0.39,0.580000000000
是否有可以使用的图形表达式生成器或方程编辑器的 Java 开源实现? 最好有在线演示,或者至少有屏幕截图。 最佳答案 取决于方程的类型。 如果您正在考虑简单的多项式,您可以尝试 Java Expres
我有四个文本输入字段,在用户输入相关值后,我必须进行 JavaScript 计算以将它们全部相加 我使用: var total = Number(value1) + Number(value2) +
为什么这段代码有两个不同的输出(GCC 4.5.1)(我已经评论了重要的行): int main() { bool a = 1; bool b = 1; bool c = 1;
如果标题含糊不清,我深表歉意,但我不知道如何为我的情况命名。我正在为使用 GPS 的 iPhone 编写一个应用程序。在 didUpdateLocations: 方法中,我针对任意大小的变量测试位置的
我正在尝试计算表中学生的 BMI,四舍五入到三位数: +-------+--------+--------+ | fname | weight | height | +-------+--------
我们可以使用 deSolve R 中的常微分方程 (ODE) 包,但是,我找不到解决两个嵌套 ODE 方程的方法,假设` b'(t) = beta - k*b(t); a'(t) = alpha -b
我有一个 boolean 方程,想简化它。帮忙解决一下。 bool needLoad = isA || (!isA && !isB); 之后我使用 if (needLoad){ if (
我很感兴趣,建模工具(在我的例子中是 OpenModelica 和 Dymola - 建模语言 Modelica)如何求解方程组(线性和/或非线性)。这些工具专为求解微分代数方程而设计。我知道一点将微
Julia:当我有绘图时如何找到最佳拟合曲线/方程?我有一个用 map 绘制的图,但我需要找到一个适合它的二次方程? 最佳答案 正如评论中所说,有一个情节在这里并不真正相关;只有数据本身是。您可以使用
我一直在尝试将像 100, 45 这样的输入放入文本框中,并通过单击按钮通过我的方程式运行它,但我不知道该怎么做。单击按钮后,它应该发布答案作为警报。请帮忙。谢谢。 function Rad(a, b
Julia:当我有绘图时如何找到最佳拟合曲线/方程?我有一个用 map 绘制的图,但我需要找到一个适合它的二次方程? 最佳答案 正如评论中所说,有一个情节在这里并不真正相关;只有数据本身是。您可以使用
有人可以向我解释为什么下面的代码会打印字符“u”吗? int p = 9; int q = 5; int r = p - q; double x = p; double y = q; St
我想以某种方式缩短我的 ODE 方程,因为否则代码会变得困惑。我尝试过使用辅助函数,例如这里的 fe() ,但这不起作用。下面的代码只是一个例子,欢迎任何建议!谢谢! # Import the req
我无法创建正确的文件。程序中的方程不会迭代,它只会根据请求的数量写入相同的总和。 for 循环。 #include #include #include #define LEN 256 int m
我有 2 个指向一些 Point 结构的指针。我想计算两点之间的距离(我不需要计算它的根)所以我有这个: w[0]=X[l]; w[1]=X[l+1]; d=m(w[0]->x
我有一个具有 CSV 上传功能的网站,它将 CSV 中的所有内容推送到临时表,然后分成较小的表。 目前,我有一个显示页面,在 HTML 表格中显示所有这些信息。然而,有些部分需要有公式化的表示。换句话
我是一名优秀的程序员,十分优秀!