且构网

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

将波斯转换为格里高利

更新时间:2023-02-26 12:40:01

实际上很简单:

// 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

当你打电话给 DateTime 构造函数并传入日历,它会为您转换 - 所以 dt.Year在这种情况下,2012年将会是。如果你想以另一种方式,你需要构建适当的 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.