如何从DateTime获取完整的月份名称

获取DateTime对象的月份的完整名称的正确方法是什么?
JanuaryDecember

我目前使用:

 DateTime.Now.ToString("MMMMMMMMMMMMM"); 

我知道这不是正确的做法。

使用“MMMM”自定义格式说明符 :

 DateTime.Now.ToString("MMMM"); 

你可以做mservidiobuild议 ,甚至更好,使用这个重载跟踪你的文化:

 DateTime.Now.ToString("MMMM", CultureInfo.InvariantCulture); 

如果您想要当前月份,则可以使用DateTime.Now.ToString("MMMM")获取完整的月份或DateTime.Now.ToString("MMM")以获取缩短的月份。

如果您有其他date想要获取月份string,则在将其加载到DateTime对象后,可以使用与该对象相同的function:
dt.ToString("MMMM")得到整个月份或dt.ToString("MMM")得到一个缩短的月份。

参考: 自定义date和时间格式string

或者,如果您需要文化特定的月份名称,那么您可以尝试这些: DateTimeFormatInfo.GetAbbreviatedMonthName方法
DateTimeFormatInfo.GetMonthName方法

你可以使用文化来获得你的国家的月份名称,如:

 System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("ar-EG"); string FormatDate = DateTime.Now.ToString("dddd., MMM dd yyyy, hh:MM tt", culture); 

它的

 DateTime.Now.ToString("MMMM"); 

用4 M s。

它应该只是DateTime.ToString( "MMMM" )

你不需要额外的M s。

 DateTime birthDate = new DateTime(1981, 8, 9); Console.WriteLine ("I was born on the {0}. of {1}, {2}.", birthDate.Day, birthDate.ToString("MMMM"), birthDate.Year); /* The above code will say: "I was born on the 9. of august, 1981." "dd" converts to the day (01 thru 31). "ddd" converts to 3-letter name of day (eg mon). "dddd" converts to full name of day (eg monday). "MMM" converts to 3-letter name of month (eg aug). "MMMM" converts to full name of month (eg august). "yyyy" converts to year. */