gpt4 book ai didi

.NET 如何将波斯语/波斯语日期字符串(Jalali 日历)解析为 DateTime 对象?

转载 作者:行者123 更新时间:2023-12-03 13:41:45 24 4
gpt4 key购买 nike

我遇到了几个很棒的代码库,用于将波斯(Jalali 日历)日期转换为公历日期。但是,我的原始来源是一个字符串,而不是 DateTime 对象。 .NET 框架中似乎没有官方支持使用波斯日历解析日期(如果我错了,请告诉我!)。

我的目标:

string persianDateString="1390/02/07";
DateTime persianDateTime = MyPersianParser.Parse(persianDateString, "yyyy/mm/dd");

当然,有些日期可能会使用单词名称来表示月份和星期几,所以我希望能够支持标准格式字符串协议(protocol)。

编辑:我知道典型的 DateTime.Parse 功能。无法使用波斯日历,因为 Microsoft 将其保留为不完整和/或不会修复它。如果有人能指出一些波斯日期解析代码,我将不胜感激。如果没有,我会要求有人删除问题并自己写。

最佳答案

您始终可以使用 native System.Globalization.PersianCalendar .NET 类。但这有点棘手。

考虑将这段代码从 Jalali Date(此处为 1387/03/18)转换为 Gregorian DateTime:

System.Globalization.PersianCalendar persianCal = new System.Globalization.PersianCalendar();
DateTime GregorianDate = persianCal.ToDateTime(1387, 3, 18, 12, 0, 0, 0);

以及将公历日期时间(此处为 1983/08/03)转换为波斯日期字符串的以下代码:
DateTime GregorianDate = DateTime.Parse("1983/08/03");
string FarsiDateConverted = persianCal.GetYear(GregorianDate).ToString("0000") + "/" +
persianCal.GetMonth(GregorianDate).ToString("00") + "/" +
persianCal.GetDayOfMonth(GregorianDate).ToString("00");

只是@Dan Bailiff 提供的链接的注释我应该重复文章作者的话:

"The main purpose for using this JalaiCalendar in place of the .NET Framework's PersianCalendar must be the need of date conversation for historical events. If you just want to display the current date in your website, PersianCalendar is sufficient."



事实上 .NET Framework 的算法对于 1178 年到 1634 年 Jalali(1799 到 2256 公历)是正确的

更新:

Starting with the .NET Framework 4.6 , PersianCalendar 类使用回历 太阳天文算法而不是计算日期的观察算法。这使得 PersianCalendar 实现与在伊朗和阿富汗使用的波斯历保持一致,这两个国家使用波斯历最广泛。因此,如果您使用 >4.6 版本的 .NET Framework,则不需要任何其他库来将日期与波斯日历相互转换。

关于.NET 如何将波斯语/波斯语日期字符串(Jalali 日历)解析为 DateTime 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5811170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com