Home
Map
DateTime.Month PropertyUse the DateTime.Month property to get month strings based on format strings.
C#
This page was last reviewed on Dec 7, 2022.
DateTime.Month returns an integer. This value indicates the month in the year, from January to December. With a format string, we can get month names.
The DateTime type provides month-related functionality in the .NET Framework. We do not need to create it ourselves: to save time and increase program reliability, it is best not to.
DateTime
First program. Month is an instance property getter in the language, which means you must call it on a DateTime instance, but not with parentheses.
Property
Then It returns an integer that is 1-based: January is equal to 1, and December is equal to 12.
int
Detail 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.
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")); } }
5 May
Example 2. You may need to display the month name in a three-letter format. This is equivalent, in English, to taking a substring of the first three letters.
But Using the three Ms next to each other may be easier and clearer for your code.
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); } } }
Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan
Full month format. In this example, we display the month string in full. We need to specify four Ms next to each other in the format string.
Here The program loops over the 11 next months after the time of writing, which happens to be in February.
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); } } }
February March April May June July August September October November December January
Arrays. We may want to have a 12-element array with all the month strings in it. This would allow us to access the month string by its index (1 being equal to 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.
Array
static Array
A summary. The base class library provides powerful DateTime methods. We do not need to manually type in month names in most cases. We can enumerate all the string values.
C#VB.NETPythonGoJavaSwiftRust
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2023 Sam Allen.