Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic, Web
Expertise: Intermediate
Mar 7, 2000

Calculate the Difference Between Dates With VBScript

Browsers Targeted: Internet Explorer 4+

If you want to find out the number of days, weeks, months, etc. between two dates, you can take advantage of the VBScript 5.0 DateDiff() function. This function lets you specify two dates and an interval token to retrieve a count of the number of that interval between the two dates.

For example, suppose that you wanted to know the number of days from January 1, 2000. The code for this is quite simple:

 
<script language="VBScript">
    curDate=Now
	document.write "Current Date:"+cstr(curDate)+"<br/>"
    document.write _
        "Number of days from 1/1/2000: "+cstr(dateDiff("d",#1/1/2000#,Now))
</script>	

The token intervals are given as follows:

Interval Token Interval Meaning Function Call
N/A Now Now
yyyy Year DateDiff("yyyy",#1/1/2000#,Now)
q Quarter DateDiff("q",#1/1/2000#,Now)
m Month DateDiff("m",#1/1/2000#,Now)
y Day in Year DateDiff("y",#1/1/2000#,Now)
w weekday DateDiff("w",#1/1/2000#,Now)
ww week in year DateDiff("ww",#1/1/2000#,Now)
h Hour DateDiff("h",#1/1/2000#,Now)
n Minute DateDiff("n",#1/1/2000#,Now)
s Second DateDiff("s",#1/1/2000#,Now)
Kurt Cagle
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap