Month - VBScript
Overview
The Month command in VB Script returns the month component of a date as an integer between 1 and 12. It is commonly used for extracting the month value from a date object for calculations, comparisons, and formatting purposes.
Syntax
Month(date)
Parameters:
- date: Required. A valid date in the format “mm/dd/yyyy” or a Date object.
Options/Flags
None.
Examples
Example 1: Extracting the month from a date string
Dim myDate = "03/25/2023"
Dim month = Month(myDate)
MsgBox month 'Output: 3
Example 2: Using Month for calculations
Dim myDate1 = "01/10/2023"
Dim myDate2 = "03/15/2023"
Dim monthDiff = Month(myDate2) - Month(myDate1)
MsgBox monthDiff 'Output: 2
Common Issues
- Invalid date format: Ensure the input date is in the correct format (mm/dd/yyyy) or is a valid Date object.
- OutOfRange error: If the month value is less than 1 or greater than 12, you may get a “Value out of range” error.
Integration
The Month command can be used with other date manipulation functions, such as Year, Day, and DateValue, to perform advanced date calculations.
Related Commands
- Date: Returns a date object.
- Year: Returns the year component of a date.
- Day: Returns the day component of a date.