VBScript DatePart Function

Definition and Usage

The DatePart function can return the specified part of the given date.

Note:The firstdayofweek parameter affects the calculation using the interval symbols 'w' and 'ww'.

Syntax

DatePart(interval,date[,firstdayofweek[,firstweekofyear]])
Parameters Description
interval

Required. The unit of time interval to be calculated between date1 and date2.

The following values can be used:

  • yyyy - Year
  • q - Quarter
  • m - Month
  • y - Day of the year
  • d - Day
  • w - Day of the week
  • ww - Week
  • h - Hours
  • n - Minutes
  • s - Seconds
date Required. The date expression to be calculated.
firstdayofweek

Optional. Specifies the number of days in a week, i.e., the day of the week.

The following values can be used:

  • 0 = vbUseSystemDayOfWeek - Using the NLS API settings for regional language support.
  • 1 = vbSunday - Sunday (default)
  • 2 = vbMonday - Monday
  • 3 = vbTuesday - Tuesday
  • 4 = vbWednesday - Wednesday
  • 5 = vbThursday - Thursday
  • 6 = vbFriday - Friday
  • 7 = vbSaturday - Saturday
firstweekofyear

Optional. Specifies the first week of the year.

The following values can be used:

  • 0 = vbUseSystem - Using the NLS API settings for regional language support.
  • 1 = vbFirstJan1 - Starting from the week containing January 1st (default).
  • 2 = vbFirstFourDays - Starting from the first week of the year with at least four days.
  • 3 = vbFirstFullWeek - Starting from the first full week of the new year.

Instance

Example 1

d = #2/10/96 16:45:30#
document.write(DatePart("yyyy",d)) 'Output: 1996'
document.write(DatePart("m",d)) 'Output: 2'
document.write(DatePart("d",d)) 'Output: 10'
document.write(DatePart("h",d)) 'Output: 16'
document.write(DatePart("n",d)) 'Output: 45'
document.write(DatePart("s",d)) 'Output: 30'
document.write(DatePart("q",d)) 'Output: 1, February is the 1st quarter.'
document.write(DatePart("y",d)) 'Output: 41, February 10 is the 41st day of 1996.'
document.write(DatePart("ww",d)) 'Output: 6, February 10 is the 6th week of 1996.'
document.write(DatePart("w",d)) 'Output: 7, February 10, 1996 is the 6th week's 7th day (Saturday).'