المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Why changing the CurrentCulture.DateTimeFormat.ShortTimePattern doesn't affect the DateTime.now format



C# Programming
10-15-2009, 10:12 AM
Hi All,

See this code below:


string format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern;
Console.WriteLine("Before Changing:");
Console.WriteLine(format);
Console.WriteLine(DateTime.Now);

Console.WriteLine("After Changing:");
CultureInfo ci = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.Name);
ci.DateTimeFormat.ShortTimePattern = "hh:mm:ss tt";
Thread.CurrentThread.CurrentUICulture = ci;
Thread.CurrentThread.CurrentCulture = ci;
format = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern;
Console.WriteLine(format);
Console.WriteLine(DateTime.Now);

Console.ReadKey();

Output:
Before Changing:
h:mm tt
10/14/2009 21:47:53
After Changing:
hh:mm:ss tt
10/14/2009 21:47:53

I want the Console.WriteLine(DateTime.Now); will print the date time
now of the application with AM/PM in the end 12h format.
I dont want to use the DateTime.Now.ToString("hh:mm:ss tt") for this action.

How can I change my UI thread/main thread with a CurrentCulture that by default will make the DateTime.Now Time to be with AM/PM(12h time) at the end??

Thanks!
Ronen