开发者

DateTime custom format not as expected

开发者 https://www.devze.com 2023-04-11 12:39 出处:网络
I\'ve been using custom formats for displaying DateTimes as found here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

I've been using custom formats for displaying DateTimes as found here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

e.g.

new DateTime(2011,10,5).ToString("dd");

Returns: "05"

and:

new DateTime(2011,10,5).开发者_高级运维ToString("d MM");

Returns "5 10"

Which are both as I would expect, but:

new DateTime(2011,10,5).ToString("d");

Returns: "05/10/2011"

When I would have expected it to return: "5"

This doesn't matter in practise as you would use: new DateTime(2011,10,5).Day to get the day, but I am interested as to why the custom format didn't work as I would have expected.


Try reading here Standard Date and Time Format Strings "d" Short date pattern.. Yes, it was probably a little stupid to reuse the d :-), fortunately it's a corner case.

In general you should always use the CultureInfo.InvariantCulture, for things that aren't input/output to the user, unless you know EXACTLY what you are doing and you know your program won't be sold abroad. So if you are showing a date to the user, use his culture/the local culture. If you are asking a date to the user the same. But if you are using the date to name a file or to do some internal operations, normally it's better to use CultureInfo.InvariantCulture.

Try this:

Console.WriteLine(DateTime.Now.ToString("MM", new System.Globalization.CultureInfo("ar-SA")));
Console.WriteLine(DateTime.Now.ToString("yyyy", new System.Globalization.CultureInfo("ar-SA")));

Console.WriteLine(DateTime.Now.ToString("MM", System.Globalization.CultureInfo.InvariantCulture));
Console.WriteLine(DateTime.Now.ToString("yyyy", System.Globalization.CultureInfo.InvariantCulture));

and tell me what you see :-)


As xanatos said, it's treating "d" as a standard date and time format string.

If you want a custom date and time format string of just "d", you need to effectively escape it to say "don't treat this as a standard pattern!"

new DateTime(2011, 10, 5).ToString("%d");

The % here is the "escape from standard to custom" part.

From the Custom Date and Time Format Strings documentation, sectino "using single custom format specifiers":

A custom date and time format string consists of two or more characters. Date and time formatting methods interpret any single-character string as a standard date and time format string. If they do not recognize the character as a valid format specifier, they throw a FormatException. For example, a format string that consists only of the specifier "h" is interpreted as a standard date and time format string. However, in this particular case, an exception is thrown because there is no "h" standard date and time format specifier.

To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before or after the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.

For example, "%h" is interpreted as a custom date and time format string that displays the hour represented by the current date and time value. You can also use the " h" or "h " format string, although this includes a space in the result string along with the hour.

0

精彩评论

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

关注公众号