将日期从波斯语转换为格里高利语

Mah*_*ari 37 .net c# datetime calendar

如何使用System.globalization.PersianCalendar将波斯日期转换为格里高利日期?请注意,我想转换我的波斯日期(例如今天是1391/04/07)并获得格里高利日期结果,在这种情况下将是06/27/2012.我正在数秒钟回答......

Jon*_*eet 70

它实际上非常简单:

// 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
Run Code Online (Sandbox Code Playgroud)

当您调用DateTime构造函数并传入a时Calendar,它会为您转换它 - dt.Year在这种情况下将是2012.如果你想走另一条路,你需要构建适当的DateTime然后使用Calendar.GetYear(DateTime)等.

简短但完整的计划:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

打印06/27/2012 00:00:00.


Moh*_*ori 11

您可以使用此代码将波斯日期转换为格里高利.

// Persian Date
var value = "1396/11/27";
// Convert to Miladi
DateTime dt = DateTime.Parse(value, new CultureInfo("fa-IR"));
// Get Utc Date
var dt_utc = dt.ToUniversalTime();
Run Code Online (Sandbox Code Playgroud)

  • CultureInfo("fa-IR") 的默认日历在 Windows 7 中不是“波斯日历”。因此请确保此代码在 Windows 7 中正常工作(如果需要) (2认同)