开发者

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

How do you get a month name from an int开发者_如何学Pythoneger value?

e.g. 7 is July


// 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);


Using a custom format string:

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


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);


  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");

  }


Dude!!

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜