VBScript DateDiff Function

Definition and Usage

The DateDiff function can return the number of time intervals between two dates.

The DateDiff function is used to calculate the difference between two date and time values, the calculation method is date2 - date1.

If comparing years, regardless of the value below the month, if comparing months, regardless of the value below the day...... and so on.

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

Syntax

DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])
Parameters Description
interval

Required. The unit of time interval to calculate 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 - Hour
  • n - Minute
  • s - Second
date1,date2 Required. Date expression. Two dates to be used in the calculation.
firstdayofweek

Optional. Defines the day of the week, that is, the day of the week.

The following values can be used:

  • 0 = vbUseSystemDayOfWeek - Using the NLS (National Language Support) API settings.
  • 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 - Using the NLS (National Language Support) API settings.
  • 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 in the New Year.
  • 3 = vbFirstFullWeek - Begin van de eerste volledige week in het nieuwe jaar.

Voorbeeld

Voorbeeld 1

document.write(Date & "<br />")
document.write(DateDiff("m",Date,"12/31/2002") & "<br />")
document.write(DateDiff("d",Date,"12/31/2002") & "<br />")
document.write(DateDiff("n",Date,"12/31/2002"))

Uitvoer:

1/14/2002
11
351
505440

Voorbeeld 2

Let op in onderstaande code, date1>date2:

document.write(Date & "<br />")
document.write(DateDiff("d","12/31/2002",Date))

Uitvoer:

1/14/2002
-351

Voorbeeld 3

Hoeveel weken (beginnend op maandag),
zijn over tussen de huidige datum en 10/10/2002
document.write(Date & "<br />")
document.write(DateDiff("w",Date,"10/10/2002",vbMonday))

Uitvoer:

1/14/2002
38