C # datetime format
I want to have a specific format for my DateTime depending on the current culture.
So, I try this:
dateTime.ToString("dd/MM/yyyy hh:mm");
This is partially ok, // is replaced with a special delimiter. But the order of the day and month does not switch (like MM / dd) depending on the culture.
Usage .ToString("g")
works, but does not include leading zero.
How to do it?
I think this will do what you want:
System.Globalization.DateTimeFormatInfo format =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string dateTime = DateTime.Now.ToString(format.FullDateTimePattern);
EDIT:
You can do the following in a short date / time:
System.Globalization.DateTimeFormatInfo format =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
string dateTime = DateTime.Now.ToString(format.ShortDatePattern + " " +
format.ShortTimePattern);
source to share
Instead of using "g", you can find the format you want on the Standard Date and Time Format documentation page .
Edit: It looks like you might want CultureInfo.DateTimeFormat.ShortDatePattern , but check the parameters anyway.
source to share
This article should answer your question: Date and Time Formatting for a Specific Culture
source to share