Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get System DateTime format?

Tags:

c#

.net

datetime

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

like image 233
BreakHead Avatar asked Jun 01 '11 07:06

BreakHead


People also ask

How can I get DateTime format?

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.

What is System DateTime now?

The Now property returns a DateTime value that represents the current date and time on the local computer.

How can I format my computer date in C#?

The final call should be string sysDateFormat = CultureInfo. CurrentCulture. DateTimeFormat. ShortDatePattern; in case you are looking for the System short date format.


2 Answers

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.

like image 54
Oded Avatar answered Sep 17 '22 19:09

Oded


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; 
like image 35
RcMan Avatar answered Sep 17 '22 19:09

RcMan