The VBScript Network and Systems Administrator's Cafe

Feb 10 2009   6:11PM GMT

Easily compare dates from with in VBScript



Posted by: Jerry Lees
date, Date Comparison, DATEDIFF, Dates, VBScipt, VBScript Functions

On occasion you need to compare two dates in your scripts, this can be a real challenge when you approach the date as if it were a string and/or compare the date parts to one another. Then finding the difference can be a whole other set of challenges if your dates span several months, days, or years.

Luckily, there is a function right into VBScript that helps you compare two dates to one another and returns the difference in a unit of time that you specify! That function is the datediff function!

Below is an example that returns the number of seconds, minutes, hours, days, and years between now and February 25, 1973.

Option Explicit

Dim DateThen, DateNow

DateThen = “2/25/1973″
DateNow = Now

WScript.Echo DateDiff(“s”,DateThen, DateNow)
WScript.Echo DateDiff(“n”,DateThen, DateNow)
WScript.Echo DateDiff(“h”,DateThen, DateNow)
WScript.Echo DateDiff(“d”,DateThen, DateNow)
WScript.Echo DateDiff(“yyyy”,DateThen, DateNow)

Enjoy!

Comment on this Post

Leave a comment: