Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Get the Current Short DateTime Format in C#?

Tags:

c#

.net

How do I get the sort DateTime format of the local system, in string format, using C#?

For example: dd/MM/yyyy or MM/dd/YYYY

like image 638
GSReddy Avatar asked Sep 07 '11 08:09

GSReddy


People also ask

What is short time format?

For example, for the en-US culture, the standard short time pattern is "h:mm tt"; for the de-DE culture, it is "HH:mm"; for the ja-JP culture, it is "H:mm". Note that its value can vary depending on the .

How can I get only the date from DateTime format?

ToString() − One more way to get the date from DateTime is using ToString() extension method. The advantage of using ToString() extension method is that we can specify the format of the date that we want to fetch. DateTime. Date − will also remove the time from the DateTime and provides us the Date only.

How do I get the current date in C sharp?

C# today's date Now; Console. WriteLine(now. ToString("F")); The example prints today's date.


2 Answers

Try this:

using System.Globalization;

...
...
...

var myDTFI = CultureInfo.CurrentCulture.DateTimeFormat;

var result = myDTFI.FullDateTimePattern;
like image 97
Davide Piras Avatar answered Sep 20 '22 02:09

Davide Piras


Overkill but should do it:

CultureInfo info = null;

var thread = new Thread(() => {
    info = Thread.CurrentThread.CurrentCulture;
    return;
});

thread.Start();
thread.Join();

// info.DateTimeFormat.ShortDatePattern

Based on the fact that new threads should inherit the standard culture of the system (I consider it a missing feature: it's nearly impossible to control the culture of new threads, read here Is there a way of setting culture for a whole application? All current threads and new threads?). I'm not directly using Thread.CurrentThread.CurrentCulture because someone could have messed with it :-)

like image 36
xanatos Avatar answered Sep 21 '22 02:09

xanatos