C# DateTime.Month.ToString(“MMM”)不起作用

C# DateTime.Month.ToString(“MMM”)不起作用,c#,datetime,console-application,C#,Datetime,Console Application,我正在从SQL抓取一个日期: 2009-10-28 11:17:06.690 DateTime createdDate; createdDate = reader.GetDateTime(1); 我将其放入DateTime并用SQL中的值填充: 2009-10-28 11:17:06.690 DateTime createdDate; createdDate = reader.GetDateTime(1); 然后我想写下月份,所以我做了: var fileMonth = createdD

我正在从SQL抓取一个日期:

2009-10-28 11:17:06.690
DateTime createdDate;
createdDate = reader.GetDateTime(1);
我将其放入DateTime并用SQL中的值填充:

2009-10-28 11:17:06.690
DateTime createdDate;
createdDate = reader.GetDateTime(1);
然后我想写下月份,所以我做了:

var fileMonth = createdDate.Month.ToString("MMM");
在代码的这一点上,fileMonth现在是=到MMM,而不是Oct

我做错了什么?

这个.Month属性只是一个整数,它对日期时间格式一无所知。您所需要的就是:

var fileMonth = createdDate.ToString("MMM");
.Month属性只是一个整数,它对日期时间格式一无所知。您所需要的就是:

var fileMonth = createdDate.ToString("MMM");
这就是您需要的:

var fileMonth = createdDate.ToString("MMM");
Month属性返回一个介于1和12之间的整数

这就是您需要的:

var fileMonth = createdDate.ToString("MMM");
Month属性返回一个介于1和12之间的整数

我喜欢这样:

            if (date.Month == 10)
            {
                string OCTOBERSTRING = "Oct";
            }
它能工作

我喜欢这样:

            if (date.Month == 10)
            {
                string OCTOBERSTRING = "Oct";
            }
它可以工作,而不是:

var fileMonth = createdDate.Month.ToString("MMM");
你应使用:

var fileMonth = createdDate.ToString("MMM");
因此,请按.Month.

而不是:

var fileMonth = createdDate.Month.ToString("MMM");
你应使用:

var fileMonth = createdDate.ToString("MMM");

所以,支持.Month.

供日后参考,当自定义日期和时间格式出现问题时,请在此查看:@Slump谢谢,我会喜欢这个并记住那个。当自定义日期和时间格式出现问题时,请在此查看:@Slump谢谢,我会喜欢这个并记住那个。@JamesWilson,因为您打电话来了在int上调用ToString,您的MMM实际上是a,而在这里,对于所有其他字符,该字符将被复制到结果字符串中,而不会发生任何变化。@JamesWilson因为您在int上调用ToString,您的MMM实际上是a,而在这里,对于所有其他字符,该字符将被复制到结果字符串中,而不会发生任何变化。是的,但是它会产生大量额外的代码。如果您的语言/文化设置有其他月份的名称,这实际上不起作用。是的,这会起作用,但会产生大量额外的代码。如果您的语言/文化设置有其他月份的名称,这实际上不起作用。