How do you get a month name from an integer value?

纵然是瞬间 提交于 2020-12-10 07:32:24

问题


How do you get a month name from an integer value?

e.g. 7 is July


回答1:


// using the current culture - returns "July" for me
string x = DateTimeFormatInfo.CurrentInfo.GetMonthName(7);

// using a specific culture - returns "juillet"
string y = CultureInfo.GetCultureInfo("fr-FR").DateTimeFormat.GetMonthName(7);



回答2:


Using a custom format string:

string name = new DateTime(2010,7,1).ToString("MMMM");



回答3:


Use the DateTime object's ToString() method. "MMMM" is the long month. "MMM" is a short month code like Aug for August. The nice thing is this way, you can deal with i18n issues too if you need to.

var monthID = 7;
var monthName = new DateTime(2000, monthID, 1).ToString("MMMM");
Console.WriteLine(monthName);



回答4:


  private static string GetMonthName(int month, bool abbrev)

  {

      DateTime date = new DateTime(1900, month, 1);

      if (abbrev) return date.ToString("MMM");

      return date.ToString("MMMM");

  }



回答5:


Dude!!

just have an string array containing names of 12 months, and names[7] is it.



来源:https://stackoverflow.com/questions/3273747/how-do-you-get-a-month-name-from-an-integer-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!