// 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
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));
}
}
// 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();
/// <summary>
/// Converts a Shamsi Date To Milady Date
/// </summary>
/// <param name="shamsiDate">string value in format "yyyy/mm/dd" or "yyyy-mm-dd"
///as shamsi date </param>
/// <returns>return a DateTime in standard date format </returns>
public static DateTime? ShamsiToMilady(string shamsiDate)
{
if (string.IsNullOrEmpty(shamsiDate))
return null;
string[] datepart = shamsiDate.Split(new char[] { '/', '-' });
if (datepart.Length != 3)
return null;
// 139p/k/b validation
int year = 0, month = 0, day = 0;
DateTime miladyDate;
try
{
year = datepart[0].Length == 4 ? int.Parse(datepart[0]) :
int.Parse(datepart[2]);
month = int.Parse(datepart[1]);
day = datepart[0].Length == 4 ? int.Parse(datepart[2]) :
int.Parse(datepart[0]);
var currentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
miladyDate = new DateTime(year, month, day, new PersianCalendar());
Thread.CurrentThread.CurrentCulture = currentCulture;
}
catch
{
return null;
}
return miladyDate;
}
6条答案
按热度按时间uqcuzwp81#
其实很简单
当你调用
DateTime
构造函数并传入一个Calendar
时,它会为你转换它--所以dt.Year
在本例中是2012.如果你想反其道而行之,你需要构造合适的DateTime
,然后使用Calendar.GetYear(DateTime)
等等.简短但完整的程序:
上面写着2012年6月27日00:00:00.
ruarlubt2#
您可以使用此代码将波斯日期转换为公历。
ih99xse13#
您可以使用以下代码
nafvub8i4#
我有一个扩展方法:
For more formats and culture-specific formats
示例:将
1391/04/07
转换为06/27/2012
9jyewag05#
我测试这个代码在windows 7 & 10和运行很好,没有任何问题
vfhzx4xs6#