VBScript DatePart function
VBScript DatePart
VBScript DatePart() function returns the specific part of the given date.
Syntax
DatePart(interval,date[,firstDayOfweek[,firstWeekofYear]])
Parameter Description
| Parameter | Required/Optional | Explanation |
|---|---|---|
| Interval | Required | It takes the values listed in the below table 1 |
| date | Required | Date both positive and negative parameters |
| firstdayofweek | Optional | Specifies the first day of the week. It can have all possible values listed in table 2 |
| firstdayofyear | Optional | Specifies 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 Value | Explanation |
|---|---|
| d | This interval value represents a day of the year. |
| m | This interval value represents a month of the year |
| y | This interval value represents a year of the year |
| yyyy | This interval value represents a year |
| w | This interval value represents a weekday |
| ww | This interval value represents a week |
| q | This interval value represents a quarter |
| h | This interval value represents a hour |
| m | This interval value represents a minute |
| s | This interval value represents a second |
Table 2 : firstdayofweek Value
| Code | Constant | Explanation |
|---|---|---|
| 0 | vbUseSystemDayOfWeek | Use National Language Support (NLS) API setting |
| 1 | vbSunday | Sunday |
| 2 | vbMonday | Monday |
| 3 | vbTuesday | Tuesday |
| 4 | vbWednesday | Wednesday |
| 5 | vbThursday | Thursday |
| 6 | vbFriday | Friday |
| 7 | vbSaturday | Saturday |
Table 3 : firstdayofyear Value
| Code | Constant | Explanation |
|---|---|---|
| 0 | vbUseSystem | Use National Language Support (NLS) API setting |
| 1 | vbFirstJan1 | Start with the week in which January 1 occurs (default) |
| 2 | vbFirstFourDays | Start with the week that has at least four days in the new year |
| 3 | vbFirstFullWeek | Start 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
