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, with the calculation method being date2 - date1.

When comparing years, regardless of the value below the month, when 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]])
Parameter Description
interval

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

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 that need to be used in the calculation.
firstdayofweek

Optional. Specifies the number of days in a week, that is, which day of the week it is.

The following values can be used:

  • 0 = vbUseSystemDayOfWeek - Use the NLS API settings for the regional language.
  • 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 - Use the NLS API settings for the regional language.
  • 1 = vbFirstJan1 - From the week containing January 1st (default).
  • 2 = vbFirstFourDays - From the first week of the year that has at least four days starting from the new year.
  • 3 = vbFirstFullWeek - Börjar på den första fulla veckan i det nya året.

Exempel

Exempel 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"))

Uppgiften:

1/14/2002
11
351
505440

Exempel 2

Observera att i följande kod, date1>date2:

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

Uppgiften:

1/14/2002
-351

Exempel 3

Hur många veckor (börjar på måndag),
är kvar mellan den nuvarande datumen och 10/10/2002
document.write(Date & "<br />")
document.write(DateDiff("w",Date,"10/10/2002",vbMonday))

Uppgiften:

1/14/2002
38