I ran into the same issue. What I needed to do was add a reference at the top of the class and change the CultureInfo of the thread that is currently executing.
using System.Threading;
string cultureName = "fr-CA";
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
DateTime theDate = new DateTime(2015, 11, 06);
theDate.ToString("g");
Console.WriteLine(theDate);
All you have to do is change the culture name, for example:
"en-US" = United States
"fr-FR" = French-speaking France
"fr-CA" = French-speaking Canada
etc...
Better yet, use just
or
to use the format the user prefers.
Look up "format strings" on MSDN to see all formatting options.
Use
yy
,yyyy
,M
,MM
,MMM
,MMMM
,d
,dd
,ffffd
,ffffdd
for the date componentUse
h
,hh
,H
,HH
,m
,mm
,s
,ss
for the time-of-day componentTry this :
I ran into the same issue. What I needed to do was add a reference at the top of the class and change the CultureInfo of the thread that is currently executing.
All you have to do is change the culture name, for example: "en-US" = United States "fr-FR" = French-speaking France "fr-CA" = French-speaking Canada etc...
I think this is simple as you can convert to and from any format without any confusion
It's almost the same, simply use the
DateTime.ToString()
method, e.g:Or:
In addition, you might want to consider using one of the predefined date/time formats, e.g:
These ensure that the format will be correct, independent of the current locale settings.
Check the following MSDN pages for more information
Some additional, related information:
If you want to display a date in a specific locale / culture, then there is an overload of the
ToString()
method that takes anIFormatProvider
:Or alternatively, you can set the
CultureInfo
of the current thread prior to formatting a date: