I am looking for a solution to get system date time format.
For example: if I get DateTime.Now
? Which Date Time Format is this using? DD/MM/YYYY
etc
You can use dd-MMM-yy hh:mm:ss tt format instead. Here an example in LINQPad. string s = "16-Aug-78 12:00:00 AM"; var date = DateTime. ParseExact(s, "dd-MMM-yy hh:mm:ss tt", CultureInfo.
The Now property returns a DateTime value that represents the current date and time on the local computer.
The final call should be string sysDateFormat = CultureInfo. CurrentCulture. DateTimeFormat. ShortDatePattern; in case you are looking for the System short date format.
If it has not been changed elsewhere, this will get it:
string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
If using a WinForms app, you may also look at the UICulture
:
string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
Note that DateTimeFormat
is a read-write property, so it can be changed.
The answers above are not fully correct.
I had a situation that my Main thread and my UI thread were forced to be in "en-US" culture (by design). My Windows DateTime format was "dd/MM/yyyy"
string sysFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern; string sysUIFormat = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;
returned "MM/dd/yyyy", but I wanted to get my real Windows format. The only way I was able to do so is by creating a dummy thread.
System.Threading.Thread threadForCulture = new System.Threading.Thread(delegate(){} ); string format = threadForCulture.CurrentCulture.DateTimeFormat.ShortDatePattern;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With