开发者

Finding out the DateTimeFormat obtained when DateTime.ToString( ) is called

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 开发者_如何学JAVAnot 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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜