简体   繁体   English

DateTime自定义格式不符合预期

[英]DateTime custom format not as expected

I've been using custom formats for displaying DateTimes as found here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx 我一直在使用自定义格式显示DateTimes,如下所示: http//msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

eg 例如

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

Returns: "05" 返回: "05"

and: 和:

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

Returns "5 10" 返回"5 10"

Which are both as I would expect, but: 这两者都是我所期望的,但是:

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

Returns: "05/10/2011" 返回: "05/10/2011"

When I would have expected it to return: "5" 当我预料到它会回来时: "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. 这在实践中并不重要,因为你会使用: new DateTime(2011,10,5).Day 。得到这一天,但我感兴趣的是为什么自定义格式不能像我预期的那样工作。

Try reading here Standard Date and Time Format Strings "d" Short date pattern. 请在此处阅读标准日期和时间格式字符串 "d" Short date pattern. . Yes, it was probably a little stupid to reuse the d :-), fortunately it's a corner case. 是的,重复使用d :-)可能有点愚蠢,幸运的是它是一个极端的案例。

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. 一般情况下,您应该始终使用CultureInfo.InvariantCulture来处理未输入/输出给用户的内容,除非您完全知道自己在做什么,并且知道您的程序不会在国外销售。 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 . 但是如果您使用日期来命名文件或进行一些内部操作,通常最好使用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. 正如xanatos所说,它将“d”视为标准的日期和时间格式字符串。

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!" 如果你想要一个只有“d”的自定义日期和时间格式字符串,你需要有效地逃避它,说“不要把它当作标准模式!”

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": 自定义日期和时间格式字符串文档 ,sectino“使用单个自定义格式说明符”:

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. 如果他们不将该字符识别为有效的格式说明符,则会抛出FormatException。 For example, a format string that consists only of the specifier "h" is interpreted as a standard date and time format string. 例如,仅由说明符“h”组成的格式字符串被解释为标准日期和时间格式字符串。 However, in this particular case, an exception is thrown because there is no "h" standard date and time format specifier. 但是,在这种特殊情况下,抛出异常,因为没有“h”标准日期和时间格式说明符。

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. 使用任何自定义日期和时间格式说明符作为格式字符串中的唯一说明符(即使用“d”,“f”,“F”,“g”,“h”,“H”, “K”,“m”,“M”,“s”,“t”,“y”,“z”,“:”或“/”自定义格式说明符)包括在之前或之后的空格说明符,或在单个自定义日期和时间说明符之前包含百分比(“%”)格式说明符。

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. 例如,“%h”被解释为自定义日期和时间格式字符串,显示当前日期和时间值表示的小时。 You can also use the " h" or "h " format string, although this includes a space in the result string along with the hour. 您也可以使用“h”或“h”格式字符串,但这会在结果字符串中包含空格以及小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM