I use System.Globalization
like below:
CultureInfo calture = new CultureInfo(MyCultureInfoGoesHere);
DateTimeFormatInfo info = calture.DateTimeFormat;
info.AbbreviatedDayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.ShortestDayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.DayNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla" };
info.AbbreviatedMonthNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "" };
info.MonthNames = new string[] { "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "bla", "" };
info.AMDesignator = "bla";
info.PMDesignator = "bla";
info.ShortDatePattern = "yyyy/MM/dd";
//info.ShortDatePattern = "dd/MM/yyyy";
info.LongDatePattern = "dddd , dd MMMM yyyy";
info.FirstDayOfWeek = DayOfWeek.bla;
MyCalendar cal = new MyCalendar();
typeof(DateTimeFormatInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(info, cal);
//object obj = typeof(DateTimeFormatInfo).GetField("m_cultureTableRecord", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).GetValue(info);
//obj.GetType().GetMethod("UseCurrentCalendar", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(obj, new object[] { cal.GetType().GetProperty("ID", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(cal, null) });
typeof(System.Globalization.CultureInfo).GetField("calendar", BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic).SetValue(calture, cal);
Thread.CurrentThread.CurrentCulture = calture;
Thread.CurrentThread.CurrentUICulture = calture;
CultureInfo.CurrentCulture.DateTimeFormat = info;
CultureInfo.CurrentUICulture.DateTimeFormat = info;
My questions are:
Why do the commented lines have errors in VS2010 (.NET 4.0), but they're ok in VS2008 (.NET 3.5)?
How can I localize my digits when I use
DateTime.Now
, I mean I want digits format in my language(Just For DateTime)! is it possible?
Thanks in advance and best regards.
Please take a look at this answer. Also that one could be of help. The basic idea is to create an extension method that will replace numbers with its Eastern Arabic counterparts. BTW. It seems that both numbers and some month / day names are already defined for the ps-AF culture:
I just don't know if it is appropriate for you.
Things change. Stuff happens. Life goes on. ~ Elizabeth Scott
Here is an example on how to set datetime info.
Now if you would print out
DateTime.Now
You would get:
I assume, this is what you wanted - get different time format, just by changing culture.
You're using reflection, which is relying on implementation details. Those details may well have changed between .NET 3.5 and .NET 4. Don't do that. If the API designers wanted you to mess with those things, they'd have made them public. Any time you rely on implementation details like this, you should expect potential breakage when changing implementation.
Have you tried cloning the whole
CultureInfo
, and modifying theNumberFormatInfo
as well as theDateTimeFormatInfo
? I can't say I've ever done that myself, but it's worth a try.