我有一个类型为 DateTime 的对象,如下所示:
var date = new DateTime(1999,1,18);
然后我用这样的 fa-IR 文化信息调用 ToString 方法:
var strDate = date.ToString("yyyy/MM/dd",CultureInfo.GetCultureInfo("fa-IR"));
我希望 strDate 为“ 1377/10/28” ,相当于 “ 1999/1/18” > fa-IR 文化。但是在调用ToString方法后,我得到“ 1999/1/18” ,这不是true。
任何人都知道问题出在哪里以及如何解决?
答案 0 :(得分:1)
您需要覆盖文化日历,并且要接收波斯语名称,也需要覆盖它们。...
或多或少是这样的:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
namespace Ravis24.Helpers
{
public class PersianCulture : CultureInfo
{
private readonly System.Globalization.Calendar cal;
private readonly System.Globalization.Calendar[] optionals;
public PersianCulture()
: this("fa-IR", true)
{ }
public PersianCulture(string cultureName, bool useUserOverride)
: base(cultureName, useUserOverride)
{
cal = base.OptionalCalendars[0];
var optionalCalendars = new List<System.Globalization.Calendar>();
optionalCalendars.AddRange(base.OptionalCalendars);
optionalCalendars.Insert(0, new PersianCalendar());
Type formatType = typeof(DateTimeFormatInfo);
Type calendarType = typeof(System.Globalization.Calendar);
PropertyInfo idProperty = calendarType.GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic);
FieldInfo optionalCalendarfield = formatType.GetField("optionalCalendars", BindingFlags.Instance | BindingFlags.NonPublic);
var newOptionalCalendarIDs = new Int32[optionalCalendars.Count];
for (int i = 0; i < newOptionalCalendarIDs.Length; i++)
newOptionalCalendarIDs[i] = (Int32)idProperty.GetValue(optionalCalendars[i], null);
optionalCalendarfield.SetValue(DateTimeFormat, newOptionalCalendarIDs);
optionals = optionalCalendars.ToArray();
cal = optionals[0];
DateTimeFormat.Calendar = optionals[0];
DateTimeFormat.MonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.MonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.AbbreviatedMonthNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.AbbreviatedMonthGenitiveNames = new[] { "فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند", "" };
DateTimeFormat.AbbreviatedDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
DateTimeFormat.ShortestDayNames = new string[] { "ی", "د", "س", "چ", "پ", "ج", "ش" };
DateTimeFormat.DayNames = new string[] { "یکشنبه", "دوشنبه", "ﺳﻪشنبه", "چهارشنبه", "پنجشنبه", "جمعه", "شنبه" };
DateTimeFormat.AMDesignator = "ق.ظ";
DateTimeFormat.PMDesignator = "ب.ظ";
DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
DateTimeFormat.ShortTimePattern = "HH:mm";
DateTimeFormat.LongTimePattern = "HH:mm:ss";
DateTimeFormat.FullDateTimePattern = "yyyy/MM/dd HH:mm:ss";
}
public override System.Globalization.Calendar Calendar
{
get { return cal; }
}
public override System.Globalization.Calendar[] OptionalCalendars
{
get { return optionals; }
}
public static DateTime PersianToGregorianUS(DateTime faDate)
{
return new PersianCalendar().ToDateTime(faDate.Year, faDate.Month, faDate.Day, faDate.Hour, faDate.Minute, faDate.Second, faDate.Millisecond);
}
}
}
答案 1 :(得分:0)
尝试一下:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fa-IR");
var date = new DateTime(1999, 1, 18);
var strDate = date.ToString("yyyy/MM/dd", CultureInfo.GetCultureInfo("fa-IR"));
Console.WriteLine(strDate);
Console.ReadKey();
这将给出输出“ 1377/10/28”