且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将CurrentCulture格式的日期时间转换为公历日期时间?

更新时间:2023-02-03 15:36:19

没有这样的东西作为波斯 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);