Get names of the months spelled out in .NET with C# / VB
In "Get the current date", we used the DateTime construct to easily obtain current date and time information - here's how you get the numerical month, from 1 to 12 (not zero-based!) intmo = DateTime.Now.Month; // an integer is returned MessageBox.Show( mo.ToString() ); // 4
You can use a zero-based month array of strings to access months names in users' language: string mo = ""; mo = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames; // Because of zero-based index, 'mo' now contains the string "February"
To get the full month name spelled out ("January", "February"...), pass a specific format provider to the ToString() method - the result depends on your Windows regional and language settings: MessageBox.Show("This month's name is " + DateTime.Now.ToString("MMMM")); // Format provider is case-sensitive - pass four *uppercase* M's! ↑
To get the month's three-letter abbreviation ("JAN", "FEB"...), just run the returned value through ToString() with a custom format provider - same as above, but three M's only: MessageBox.Show("Month's abbreviation: " + DateTime.Now.ToString("MMM")); // Use the ToUpper() method to return the abbreviation in uppercase