C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: It returns an integer that is 1-based: January is equal to 1, and December is equal to 12.
Int, uintFormat: The example code uses the MMM format string for short, 3-letter month strings.
Also: You can use the MMMM string (four Ms) for the complete month name. So Sep turns into September.
C# program that uses Month
using System;
class Program
{
static void Main()
{
//
// Get the current month integer.
//
DateTime now = DateTime.Now;
//
// Write the month integer and then the three-letter month.
//
Console.WriteLine(now.Month);
Console.WriteLine(now.ToString("MMM"));
}
}
Output
5
May
But: Using the three Ms next to each other may be easier and clearer for your code.
C# program that displays months
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
for (int i = 0; i < 12; i++)
{
Console.WriteLine(now.ToString("MMM"));
now = now.AddMonths(1);
}
}
}
Output
Feb
Mar
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Jan
Here: The program loops over the 11 next months after the time of writing, which happens to be in February.
C# program that displays complete months
using System;
class Program
{
static void Main()
{
DateTime now = DateTime.Now;
for (int i = 0; i < 12; i++)
{
Console.WriteLine(now.ToString("MMMM"));
now = now.AddMonths(1);
}
}
}
Output
February
March
April
May
June
July
August
September
October
November
December
January
And: This may require a 13-element array. Using a static array to cache strings is often faster than accessing them from the framework.
ArrayStatic Array