将日期从波斯语转换为格里高利语
如何使用System.globalization.PersianCalendar将波斯日期转换为格里高利日期? 请注意,我想转换我的波斯日期(例如今天是1391/04/07)并获得格里高利日期结果,在这种情况下将是06/27/2012。 我正在数秒钟回答……
它实际上非常简单:
// 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
构造函数并传入一个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)); } }
打印06/27/2012 00:00:00。
你应该做的只是定义日历:
DateTime dt = new DateTime(year, mon, day, new PersianCalendar());
您可以使用此代码将波斯日期转换为格里高利。
// 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();