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

我目前使用:

DateTime.Now.ToString("MMMMMMMMMMMMM");

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


当前回答

您可以使用系统中的CultureInfo。全球化来获取数据,基于当前正在使用的文化。

_ = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month)

或者,使用InvariantCulture来获得英文名称。

_ = CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);

其他回答

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

DateTime.Now.ToString("MMMM");

It's

DateTime.Now.ToString("MMMM");

有4个Ms。

如果您接收到“MMMM”作为响应,那么您可能正在获取月份,然后将其转换为定义格式的字符串。

DateTime.Now.Month.ToString("MMMM") 

将输出"MMMM"

DateTime.Now.ToString("MMMM") 

是否输出月份名称

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

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

您可以使用系统中的CultureInfo。全球化来获取数据,基于当前正在使用的文化。

_ = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month)

或者,使用InvariantCulture来获得英文名称。

_ = CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);