DateTime.Month
This VB.NET Property returns a value between and 0 and 11 inclusive, and it indicates the month represented in the DateTime
structure. For January, we get the value 0.
Meanwhile, for displaying names of months, we can use the ToString
Function on a DateTime
instance with the "MMM" or "MMMM" format codes. In this way we handle months in VB.NET programs.
DateTime
is a Structure
, and it has many useful properties and methods upon it. The Month is a Property—we access it without using parentheses. We can read it, but not write to it.
DateTime
that represents the current moment. Then we print the Month value, and the month string
.AddMonths
in a loop. We print the short
month strings.string
, we must specify the format string
as "MMMM." A lot of "Ms" are necessary here.Module Module1 Sub Main() ' Part 1: After getting Now, access the Month integer and the 3-letter month string. Dim now As DateTime = DateTime.Now Console.WriteLine(now.Month) Console.WriteLine(now.ToString("MMM")) ' Part 2: Loop over all possible months. now = DateTime.Now For i = 0 To 11 Console.Write("{0} ", now.ToString("MMM")) now = now.AddMonths(1) Next Console.WriteLine() ' Part 3: Full month format has 4 uppercase M chars. now = DateTime.Now For i = 0 To 11 Console.Write("{0} ", now.ToString("MMMM")) now = now.AddMonths(1) Next Console.WriteLine() End Sub End Module7 Jul Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun July August September October November December January February March April May June
With the Month property, as well the ToString
Function with "MMM" and "MMMM", we handle months. Not only this, but AddMonths()
can be used to modify months.