tags:

views:

33

answers:

3

I have the following issue with handling datetime formats for a component I am developing.

string value = DateTime.Now.ToString( ); // value = "8/3/2010 2:20:49 PM"

How do I find out which date format value refers to --> "M/d/yyyy h:mm:ss tt"

I wish to store this current datetime format during export and use the same during import. Various apis available in DateTimeFormatInfo.CurrentInfo and CultureInfo.CurrentCulture.DateTimeFormat do not provide this info.

One solution I know of is to use dt.ToString( "u" ) to store and parse datetime in universal format, but I am curious how I could get the above format.

+1  A: 

If you want reliable import / export of DateTimes, it is best to set the used format specified explicitly, e.g. using the invariant culture.

tdammers
yeah. I am using that, but wanted to know if I could obtain the current datetime format.
Bharath K
See Kangkan's answer. The LongDatePattern and LongTimePattern properties of the current culture will provide you with this information.
tdammers
+1  A: 

Check out Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern for the current datetime format.

Kangkan
+2  A: 

Per the documentation:

DateTime.ToString()

The value of this instance is formatted using the general format specifier, 'G', as described in the Formatting Overview topic. The return value is identical to the value returned by ToString ("G", null).

Following the doc links Formatting Overview -> Date And Time Format Strings -> Standard DateTime Format Strings gets us to

G

General date/time pattern (long time)

Displays a combination of the short date and long time patterns, separated by a space.

So the format you want should be obtainable by combining the ShortDatePattern and LongTimePattern members of Thread.CurrentThread.CurrentCulture.DateTimeFormat.

AakashM