如何将CurrentCulture格式的日期时间转换为公历日期时间? [英] How to convert a datetime in CurrentCulture format to Gregorian DateTime?
问题描述
我将 Global.asax
设置为:
protected void Application_BeginRequest(object sender, EventArgs e)
{
PersianCulture fa = new PersianCulture();
Thread.CurrentThread.CurrentCulture = fa;
}
我想将 1392 1 23转换为 2013 4 12。
I want to convert "1392 1 23" to "2013 4 12".
我该怎么办?
推荐答案
没有这样的东西作为波斯 DateTime
。 DateTime
值始终是公历日历值,没有特定格式。设置格式时(通常通过调用 ToString
),可以确定其格式-如果您使用的文化使用非格里高利历法,它将转换原始值
There's no such thing as "a Persian DateTime
". A DateTime
value is always a Gregorian calendar value, with no specific formatting. When you format it (usually by calling ToString
) you can determine how it's formatted - and if you use a culture which uses a non-Gregorian calendar, it will convert the original value into that calendar.
因此,例如,如果您想解析波斯用户的输入,然后将其转换为英语用户可以理解的等效日期,则可以使用:
So for example, if you wanted to parse input from a Persian user and then convert that to the equivalent date that an English user would understand, you could use:
DateTime date = DateTime.Parse(text, persianCulture);
string englishText = date.ToString(englishCulture);
这篇关于如何将CurrentCulture格式的日期时间转换为公历日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!