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. Defines the number of days in a week, i.e., the day of the week.

The following values can be used:

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

Optional. Defines the first week of the year.

The following values can be used:

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

Exempel

Exempel 1

d = #2/10/96 16:45:30#
document.write(DatePart("yyyy",d)) 'Uttan: 1996'
document.write(DatePart("m",d)) 'Uttan: 2'
document.write(DatePart("d",d)) 'Uttan: 10'
document.write(DatePart("h",d)) 'Uttan: 16'
document.write(DatePart("n",d)) 'Uttan: 45'
document.write(DatePart("s",d)) 'Uttan: 30'
document.write(DatePart("q",d)) 'Uttan: 1, februari är det 1:a kvartalet'
document.write(DatePart("y",d)) 'Uttan: 41, 10 februari är den 41:e dagen 1996.'
document.write(DatePart("ww",d)) 'Uttan: 6, 10 februari är den 6:e veckan 1996.'
document.write(DatePart("w",d)) 'Uttan: 7, 10 februari är den 6:e veckan den 7:e dagen 1996 (lördag).'