.NET如何将Persian / Farsi日期字符串(Jalali日历)解析为DateTime对象? [英] .NET How to parse a Persian / Farsi date string (Jalali calendar) into a DateTime object?

查看:189
本文介绍了.NET如何将Persian / Farsi日期字符串(Jalali日历)解析为DateTime对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经运行了几个伟大的代码库,用于将波斯文(Jalali日历)日期转换为格里高利日期。但是,我的原始源是一个字符串,而不是一个DateTime对象。在.NET框架中似乎没有正式支持使用波斯语解析日期(如果我错了,请告诉我!)。

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!).

我的目标:

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

当然,有些日期可能会使用一个月的几个月和几个星期的字词, d喜欢能够支持标准格式字符串协议。

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.

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

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.

推荐答案

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

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.

这篇关于.NET如何将Persian / Farsi日期字符串(Jalali日历)解析为DateTime对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆