問題描述
如何使用 System.globalization.PersianCalendar 將波斯日期轉(zhuǎn)換為公歷日期?請注意,我想轉(zhuǎn)換我的波斯日期(例如今天是 1391/04/07)并獲得公歷日期結(jié)果,在這種情況下將是 06/27/2012.我正在數(shù)秒來回答...
How can I convert Persian date to Gregorian date using System.globalization.PersianCalendar? Please note that I want to convert my Persian Date (e.g. today is 1391/04/07) and get the Gregorian Date result which will be 06/27/2012 in this case. I'm counting seconds for an answer ...
推薦答案
其實(shí)很簡單:
// I'm assuming that 1391 is the year, 4 is the month and 7 is the day
DateTime dt = new DateTime(1391, 4, 7, persianCalendar);
// Now use DateTime, which is always in the Gregorian calendar
當(dāng)您調(diào)用 DateTime
構(gòu)造函數(shù)并傳入 Calendar
時(shí),它會(huì)為您轉(zhuǎn)換它 - 所以 dt.Year
將是 2012 年這個(gè)案例.如果你想走另一條路,你需要構(gòu)造適當(dāng)?shù)?DateTime
然后使用 Calendar.GetYear(DateTime)
等.
When you call the DateTime
constructor and pass in a Calendar
, it converts it for you - so dt.Year
would be 2012 in this case. If you want to go the other way, you need to construct the appropriate DateTime
then use Calendar.GetYear(DateTime)
etc.
簡短而完整的程序:
using System;
using System.Globalization;
class Test
{
static void Main()
{
PersianCalendar pc = new PersianCalendar();
DateTime dt = new DateTime(1391, 4, 7, pc);
Console.WriteLine(dt.ToString(CultureInfo.InvariantCulture));
}
}
打印 06/27/2012 00:00:00.
That prints 06/27/2012 00:00:00.
這篇關(guān)于將日期從波斯語轉(zhuǎn)換為公歷的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!