VBScript DatePart function
May 13, 2021
VBScript
Table of contents
VBScript
DatePart
function
The VBScript DatePart function returns a Variant Integer that contains a specified time portion of a known date.
Complete VBScript reference manual
The DatePart function returns the specified portion of a given date.
Grammar
DatePart(interval,date[,firstdayofweek[,firstweekofyear]])
Parameters
parameter
|
describe
|
interval
|
Required.The time interval to be returned.
The following value can be used:
-
YYYY - year
-
Q - quarter
-
M - month
-
Y - the first day of the year
-
D - day
-
W - as the first few days
-
WW - the last year
-
H - hour
-
n - points
-
s - second
|
date
|
Required.Date expression needs to be calculated.
|
firstdayofweek
|
Optional.The number of days in the week, that is, the first few days of the week.
The following value can be used:
-
0 = VbusesystemDayofweek - Using Regional Language Support (NLS) API Settings
-
1 = VBSunday - Sunday (default)
-
2 = VBMONDAY - Monday
-
3 = VBTESDAY - Tuesday
-
4 = vbwednesday - Wednesday
-
5 = VBTHURSDAY - Thursday
-
6 = VBFRIDAY - Friday
-
7 = VBSATURDAY - Saturday
|
firstweekofyear
|
Optional.The first week of the year is specified.
The following value can be used:
-
0 = VbuseSystem - Use Regional Language Support (NLS) API Settings
-
1 = VBFIRSTJAN1 - starting from January 1st (default)
-
2 = vbfirstfourDays - starting with at least four days in the new year
-
3 = vbfirstfullweek - starting with the first complete week in the new year
|
Instance 1
Get the month from a date:
<script type="text/vbscript">
d=CDate("2010-02-16")
document.write(DatePart("m",d))
</script>
The above example output results:
2
Try it out . . .
Instance 2
Get the month we're in:
<script type="text/vbscript">
document.write(DatePart("m",Now()))
</script>
The above example output results:
document.write(DatePart("m",Now()))
Try it out . . .
Instance 3
Get hours:
<script type="text/vbscript">
document.write(DatePart("h",Now())
</script>
The above example output results:
document.write(DatePart("h",Now()))
Try it out . . .
Complete VBScript reference manual