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
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 .
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.
C# today's date Now; Console. WriteLine(now. ToString("F")); The example prints today's date.
Try this:
using System.Globalization;
...
...
...
var myDTFI = CultureInfo.CurrentCulture.DateTimeFormat;
var result = myDTFI.FullDateTimePattern;
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 :-)
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