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

发布于 2024-11-03 17:40:55 字数 439 浏览 4 评论 0原文

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

我的目标:

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

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

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

I've run across several great code libraries for converting a Persian (Jalali calendar) date to a Gregorian date. However, my original source is a string, not a DateTime object. There doesn't seem to be official support in the .NET framework for parsing dates using the Persian calendar (if I'm wrong, please show me!).

My goal:

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

And of course, some dates may use word names for months and days of the week, so I'd like to be able to support the standard format string protocol.

EDIT: I know about the typical DateTime.Parse functionality. The Persian calendar cannot be used because Microsoft left it incomplete and/or won't fix it. If anyone can point me to some Persian date parsing code I'd be grateful. If not, I'll request someone remove the question and just write it myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假装不在乎 2024-11-10 17:40:55

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

考虑此代码用于从 Jalali Date(此处为 1387/03/18)转换为公历日期时间:

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 我应该重复一下文章作者的话:

“使用这个 JalaiCalendar 代替 .NET Framework 的 PersianCalendar 的主要目的肯定是需要历史事件的日期对话。如果您只想在网站中显示当前日期,PersianCalendar 就足够了。”

事实上,.NET Framework 的算法对于 1178 年至 1634 年 Jalali(1799 年至 2256 年公历)

更新是正确的:

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

You can always use the native System.Globalization.PersianCalendar .NET class. but it is a bit tricky.

Consider this code for converting from Jalali Date (here 1387/03/18) to Gregorian DateTime:

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

and the following code to convert a Gregorian DateTime (here 1983/08/03) to Persian Date string:

DateTime GregorianDate = DateTime.Parse("1983/08/03");
string FarsiDateConverted = persianCal.GetYear(GregorianDate).ToString("0000") + "/" +
             persianCal.GetMonth(GregorianDate).ToString("00") + "/" +
             persianCal.GetDayOfMonth(GregorianDate).ToString("00");

just a note for the link provided by @Dan Bailiff I should repeat the author of the article's words:

"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."

In fact algorithm of .NET Framework is correct for the years 1178 to 1634 Jalali (1799 to 2256 Gregorian)

Update:

Starting with the .NET Framework 4.6, the PersianCalendar class uses the Hijri solar astronomical algorithm rather than an observational algorithm to calculate dates. This makes the PersianCalendar implementation consistent with the Persian calendar in use in Iran and Afghanistan, the two countries in which the Persian calendar is in most widespread use. So if you use >4.6 version of .NET Framework you don't need any other libraries for converting dates to/from Persian Calendar.

善良天后 2024-11-10 17:40:55

为了解析日期字符串,我只需使用默认日历来获取日期值(年、月、日等),

然后使用 这里库 将波斯日期值转换为公历日期值。

我的代码现在看起来像这样:

string persianDate = "1390/02/07";
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime persianDateTime = DateTime.ParseExact(persianDate, "yyyy/MM/dd", persianCulture);    // this parses the date as if it were Gregorian

JalaliCalendar jc = new JalaliCalendar();
// convert the Persian calendar date to Gregorian
DateTime gregorianDateTime = jc.ToDateTime(persianDateTime.Year, persianDateTime.Month, persianDateTime.Day, persianDateTime.Hour, persianDateTime.Minute, persianDateTime.Second, persianDateTime.Millisecond);

当然,我必须自己处理带有名称(月份、星期几)的日期组件,但我可以很容易地处理它。

For parsing the date string, I simply use the default calendar to get the date values (year, month, day, etc.)

I then use this library here to convert the Persian date values to the Gregorian date values.

My code now looks like this:

string persianDate = "1390/02/07";
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime persianDateTime = DateTime.ParseExact(persianDate, "yyyy/MM/dd", persianCulture);    // this parses the date as if it were Gregorian

JalaliCalendar jc = new JalaliCalendar();
// convert the Persian calendar date to Gregorian
DateTime gregorianDateTime = jc.ToDateTime(persianDateTime.Year, persianDateTime.Month, persianDateTime.Day, persianDateTime.Hour, persianDateTime.Minute, persianDateTime.Second, persianDateTime.Millisecond);

Of course, I'll have to take care of date components with names (months, days of the week) myself, but I can deal with that pretty easily.

满栀 2024-11-10 17:40:55

或者:

using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("fa-IR");
      string MyString = "1390/02/07";
      DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo);

还有更多例子:
http://msdn.microsoft.com/en-us/library/2h3syy57 .aspx#Y418

Or:

using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("fa-IR");
      string MyString = "1390/02/07";
      DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo);

There is more example:
http://msdn.microsoft.com/en-us/library/2h3syy57.aspx#Y418

开始看清了 2024-11-10 17:40:55

在 ASP.NET MVC 中尝试验证波斯日期

 [RegularExpression(@"^$|^([1۱][۰-۹ 0-9]{3}[/\/]([0 ۰][۱-۶ 1-6])[/\/]([0 ۰][۱-۹ 1-9]|[۱۲12][۰-۹ 0-9]|[3۳][01۰۱])|[1۱][۰-۹ 0-9]{3}[/\/]([۰0][۷-۹ 7-9]|[1۱][۰۱۲012])[/\/]([۰0][1-9 ۱-۹]|[12۱۲][0-9 ۰-۹]|(30|۳۰)))$", ErrorMessage = "Not Validate")]

try it in ASP.NET MVC for validate Persian date

 [RegularExpression(@"^$|^([1۱][۰-۹ 0-9]{3}[/\/]([0 ۰][۱-۶ 1-6])[/\/]([0 ۰][۱-۹ 1-9]|[۱۲12][۰-۹ 0-9]|[3۳][01۰۱])|[1۱][۰-۹ 0-9]{3}[/\/]([۰0][۷-۹ 7-9]|[1۱][۰۱۲012])[/\/]([۰0][1-9 ۱-۹]|[12۱۲][0-9 ۰-۹]|(30|۳۰)))$", ErrorMessage = "Not Validate")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文