在波斯语中打印c#DateTime最简单的方法是什么?目前我正在使用:
static public string PersianDateString(DateTime d)
{
CultureInfo faIR = new CultureInfo("fa-IR");
faIR.DateTimeFormat.Calendar = new PersianCalendar();
return d.ToString("yyyy/MM/dd", faIR);
}
Run Code Online (Sandbox Code Playgroud)
这引发了异常
不是给定文化的有效日历
我遇到了几个很棒的代码库,用于将波斯语(Jalali日历)日期转换为格里高利日期.但是,我的原始源是一个字符串,而不是DateTime对象.在.NET框架中似乎没有官方支持使用波斯语日历解析日期(如果我错了,请告诉我!).
我的目标:
string persianDateString="1390/02/07";
DateTime persianDateTime = MyPersianParser.Parse(persianDateString, "yyyy/mm/dd");
Run Code Online (Sandbox Code Playgroud)
当然,有些日期可能会在一周中的几个月和几天使用单词名称,因此我希望能够支持标准格式字符串协议.
编辑:我知道典型的DateTime.Parse功能.无法使用波斯日历,因为Microsoft将其保留为不完整和/或无法修复它.如果有人能指出一些波斯语日期解析代码,我将不胜感激.如果没有,我会请求有人删除问题,然后自己写.
我想转换persianDate成standarddate.
public DateTime ConvertPersianToEnglish(string persianDate)
{
string[] formats = { "yyyy/MM/dd", "yyyy/M/d", "yyyy/MM/d", "yyyy/M/dd" };
DateTime d1 = DateTime.ParseExact(persianDate, formats,
CultureInfo.CurrentCulture, DateTimeStyles.None);
PersianCalendar persian_date = new PersianCalendar();
DateTime dt = persian_date.ToDateTime(d1.Year, d1.Month, d1.Day, 0, 0, 0, 0, 0);
return dt;
}
Run Code Online (Sandbox Code Playgroud)
在Persiandate具有格式是这样的:1392/10/12(Year/month/day)
在我的应用程序中,我试图转换Year/month/31为标准时间,但我收到此错误:
{System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseExactMultiple(String s, String[] formats, DateTimeFormatInfo dtfi, DateTimeStyles style)
Run Code Online (Sandbox Code Playgroud)
我得到错误的确切值是 1393/04/31