Reputation: 4032
I change my machine (win 10) date format to Persian and now when I run this code:
DateTime date = DateTime.Now;
System.Diagnostics.Debug.WriteLine(date);
I am getting this result: 19/09/1396 10:41:45 ب.ظ
how can I change this result to 2017/12/10 10:45 pm
?
And is there any config which change whole application date format to georgian?
Upvotes: 1
Views: 1571
Reputation: 4032
I finally use @Patrick Atrner guide and add this line of code to my Program.CS file and my problem solved...
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fa-IR");
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Upvotes: 1
Reputation: 51683
You need to format the date with the correct culture:
static void Main()
{
DateTime date = DateTime.Now;
System.Diagnostics.Debug.WriteLine(
date.ToString("yyyy/MM/dd hh:mm tt", System.Globalization.CultureInfo.GetCultureInfo("en-us", "en")));
Console.ReadLine();
}
I am providing a USERDEFINED format string as the us format seems to prefere - to /.
Links:
If you always need this kind of format you could cache the CultureInfo for us and wrapp the output into a function for convenience.
Upvotes: 3
Reputation: 305
If you changed just your machine's date format, then you are specifically telling it to use that format regardless of the culture, so changing the culture of the machine wont work. If you don't wan't to display dates in that format, then change your machine date format back to what you want.
Upvotes: -2