1-最初にPersiandateNuGetパッケージ(17KB)をインストールします
Install-Package PersianDate
2-PersianDateクラスのToFaおよびtoEn静的メソッドを使用します(次のサンプルでわかるように、ライブラリ自体によって処理されるセパレーターと方向について心配する必要はありません。また、GitHub内にUsageConsoleプロジェクトを見つけてテストすることもできます簡単に自分で):
//default format
string dts=ConvertDate.ToFa(DateTime.Now);//1393/08/01
//date only (short and D for Long)
dts=ConvertDate.ToFa(DateTime.Now, "d");//93/08/01
dts=ConvertDate.ToFa(DateTime.Now, "D");//پنج شنبه, 01 آبان 1393
//time only
dts=ConvertDate.ToFa(DateTime.Now, "t");//21:53
dts=ConvertDate.ToFa(DateTime.Now, "T");//21:53:26
//general short date + time
dts=ConvertDate.ToFa(DateTime.Now, "g");//93/08/01 21:53
dts=ConvertDate.ToFa(DateTime.Now, "G");//93/08/01 21:53:26
//general full date + time
dts=ConvertDate.ToFa(DateTime.Now, "f");//پنج شنبه, 01 آبان 1393 21:53
dts=ConvertDate.ToFa(DateTime.Now, "F");//پنج شنبه, 01 آبان 1393 21:53:26
//only month and year
dts=ConvertDate.ToFa(DateTime.Now, "m");//آبان 1
dts=ConvertDate.ToFa(DateTime.Now, "y");//1393 آبان
dts=ConvertDate.ToFa(DateTime.Now);//1393/08/01
// converting back to Gregorian date
Datetime dt= ConvertDate.ToEn(dts);//2014/10/23 00:00:00
3-変更および新しいフォーマットのサポートのためにGitHubで利用可能なソースコード。
次の関数のソースコードを探して、一般的にどのように機能しているかを確認してください
public static DateTime ToEn(string fadate)
{
if (fadate.Trim() == "") return DateTime.MinValue;
int[] farsiPartArray = SplitRoozMahSalNew(fadate);
return new PersianCalendar().ToDateTime(farsiPartArray[0], farsiPartArray[1], farsiPartArray[2], 0, 0, 0, 0);
}
private static int[] SplitRoozMahSalNew(string farsiDate)
{
int pYear = 0;
int pMonth = 0;
int pDay = 0;
//normalize with one character
farsiDate = farsiDate.Trim().Replace(@"\", "/").Replace(@"-", "/").Replace(@"_", "/").
Replace(@",", "/").Replace(@".", "/").Replace(@" ", "/");
string[] rawValues = farsiDate.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (!farsiDate.Contains("/"))
{
if (rawValues.Length != 2)
throw new Exception("usually there should be 2 seperator for a complete date");
}
else //mostly given in all numeric format like 13930316
{
// detect year side and add slashes in right places and continue
}
//new simple method which emcompass below methods too
try
{
pYear = int.Parse(rawValues[0].TrimStart(new[] { '0' }).Trim());
pMonth = int.Parse(rawValues[1].TrimStart(new[] { '0' }).Trim());
pDay = int.Parse(rawValues[2].TrimStart(new[] { '0' }).Trim());
// the year usually must be larger than 90
//or for historic values rarely lower than 33 if 2 digit is given
if (pYear < 33 && pYear > 0)
{
//swap year and day
pYear = pDay;
pDay = int.Parse(rawValues[0]); //convert again
}
//fix 2 digits of persian strings
if (pYear.ToString(CultureInfo.InvariantCulture).Length == 2)
pYear = pYear + 1300;
//
if (pMonth <= 0 || pMonth >= 13)
throw new Exception("mahe shamsi must be under 12 ");
}
catch (Exception ex)
{
throw new Exception(
"invalid Persian date format: maybe all 3 numric Sal, Mah,rooz parts are not present. \r\n" + ex);
}
return new[] { pYear, pMonth, pDay };
}