The VBScript Network and Systems Administrator's Cafe

Feb 10 2009   6:11PM GMT

Easily compare dates from with in VBScript

Jerry Lees Jerry Lees Profile: Jerry Lees

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

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: