views:

1203

answers:

8
+1  Q: 

c# datetime format

I wish 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, the / gets replaced by the culture-specific separator. But the day and month order are not switched (like MM/dd) depending on the culture.

Using .ToString("g") works, but that doesn't include the leading zero.

How do I accomplish this?

+4  A: 

Instead of using "g", you might find the format you want in the Standard Date and Time Format Strings documentation page.

Edit: It looks like you may want CultureInfo.DateTimeFormat.ShortDatePattern, but check out the options anyway.

Richard Szalay
+4  A: 

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 for a short date/time:

    System.Globalization.DateTimeFormatInfo format =
         System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat;
    string dateTime = DateTime.Now.ToString(format.ShortDatePattern + " " + 
         format.ShortTimePattern);
GenericTypeTea
FullDateTimePattern is "dddd, MMMM dd, yyyy h:mm:ss tt" for en-US
Richard Szalay
Good point, have provided a better example
GenericTypeTea
The output is exactly the same as dateTime.ToString("g")...
Meta-Knight
Also the same as dateTime.ToShortDateString + " " + dateTime.ToShortTimeString, both a lot shorter than your code.
Meta-Knight
A: 

This article should answer your question: Formatting Date and Time for a Specific Culture

blak3r
A: 

If you are not happy with the standard patterns, you can edit them like this:

DateTimeFormatInfo format = (DateTimeFormatInfo) CultureInfo.CurrentCulture.DateTimeFormat.Clone();
format.ShortTimePattern = "hh:mm"; // example only
string result = value.ToString("g", format);
Christian Hayter
+1  A: 

You do want to use "g", however the leading zero is dependant on the culture. The Invariant culture and some others will include the leading zero, but other cultures will not.

Any particular reason you want to maintain the leading zero?

NerdFury
+1  A: 

The / character is a placeholder that will be replaced by the date separation character of the current culture. If you wish to "hard-code" the format to use / you need to alter the format string like this:

dateTime.ToString("dd'/'MM'/'yyyy hh':'mm");
Fredrik Mörk
A: 

try this:

var cinf = CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator;

DateTime.Now.ToString("dd/MM/yyyy hh:mm").Replace(cinf,"/");
TheVillageIdiot
A: 

The problem is not the seperator, the problem is this:

When in culture nl-nl I want to display "dd-MM-yyyy hh:mm" When in culture en-us I want to display "MM/dd/yyyy hh:mm"

Now the using the / as a seperator makes sure it gets replaced by the current culture' seperator. But how do I display month or day first, depending on the culture, with a custom formatting and maintaining the leading zero?

jaap