VBScript DatePart function

VBScript DatePart

VBScript DatePart() function returns the specific part of the given date.

Syntax

DatePart(interval,date[,firstDayOfweek[,firstWeekofYear]]) 

Parameter Description

ParameterRequired/OptionalExplanation
IntervalRequiredIt takes the values listed in the below table 1
dateRequiredDate both positive and negative parameters
firstdayofweekOptionalSpecifies the first day of the week. It can have all possible values listed in table 2
firstdayofyearOptionalSpecifies the first day of the year. It can have all possible values listed in table 3

Table 1 : interval values

Below is list of all the possible values of interval parameter -

Interval ValueExplanation
dThis interval value represents a day of the year.
mThis interval value represents a month of the year
yThis interval value represents a year of the year
yyyyThis interval value represents a year
wThis interval value represents a weekday
wwThis interval value represents a week
qThis interval value represents a quarter
hThis interval value represents a hour
mThis interval value represents a minute
sThis interval value represents a second

Table 2 : firstdayofweek Value

CodeConstantExplanation
0vbUseSystemDayOfWeekUse National Language Support (NLS) API setting
1vbSundaySunday
2vbMondayMonday
3vbTuesdayTuesday
4vbWednesdayWednesday
5vbThursdayThursday
6vbFridayFriday
7vbSaturdaySaturday

Table 3 : firstdayofyear Value

CodeConstantExplanation
0vbUseSystemUse National Language Support (NLS) API setting
1vbFirstJan1Start with the week in which January 1 occurs (default)
2vbFirstFourDaysStart with the week that has at least four days in the new year
3vbFirstFullWeekStart with the first full week of the new year

Example :

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
d1 = "2024-06-22"
quarter    = DatePart("q", d1)
document.write("Date is in Quarter : " & quarter & "<br />")
dayOfYear  = DatePart("y", d1)
document.write("Day of year is : " & dayOfYear&"<br />")
weekOfYear = DatePart("ww", d1)
document.write("Week of year : " & weekOfYear & "<br />")
document.write("Month of year : " & DatePart("m",d1))
</script>
</body>
</html>

Output :

After running above script in IE we will get following output -

Date is in Quarter : 2
Day of year is : 173
Week of year : 26
Month of year : 6