VBScript Date Functions - The Vainglorious DateDiff Function
(Page 4 of 4 )
In the following example I created a program to tell the amount of years, months, minutes, and seconds since my birth:
<html>
<body>
<script type="text/vbscript">
document.write("Today's date is: " & Date & "<br />")
document.write("<br />The amount of time since my magnanimous birth can be measured in the following ways: <br /><br />")
document.write(DateDiff("yyyy",Date,"04/22/1977")& (" years") & "<br />")
document.write(DateDiff("m",Date,"04/22/1977")& (" months") & "<br />")
document.write(DateDiff("n",Date,"04/22/1977")& (" minutes") & "<br />")
document.write(DateDiff("s",Date,"04/22/1977")& (" seconds"))
</script>
</body>
</html>
The result is:
Today's date is: 12/4/2007
The amount of time since my magnanimous birth can be measured in the following ways:
-30 years
-368 months
-16103520 minutes
-966211200 seconds
In the above example we calculated how much time had passed by subtracting my birthdate from the current time (well, based on our system timer). But what if I wanted to subtract a specified date from another specified date? I could do this:
<html>
<body>
<script type="text/vbscript">
document.write("Today's date is: " & Date & "<br />")
document.write("If Jesus had lived to be a hundred ")
document.write("the difference in the date of his hundredth ")
document.write("birthday and my birthdate could be ")
document.write("measured thusly:<br /><br/>")
document.write(DateDiff("yyyy","12/25/100","04/25/1977")& (" years") & "<br />")
document.write(DateDiff("m","12/25/100","04/22/1977")& (" months") & "<br />")
document.write(DateDiff("n","12/25/100","04/22/1977")& (" minutes") & "<br />")
document.write(DateDiff("s","12/25/100","04/22/1977")& (" seconds"))
</script>
</body>
</html>
This results in the following:
Today's date is: 12/4/2007
If Jesus had lived to be a hundred the difference in the date of his hundredth birthday and my birthdate could be measured thusly:
1877 years
22516 months
986850720 minutes
59211043200 seconds
You will note that I used the year 100. My original intention was to use the date 12/25/0000 (I know Jesus wasn't REALLY born on that day) but if you use anything less than 100 for the year portion of the date, computers get confused and think you are referencing the 1900s. Crazy computers. Then the next thing you know you got yourself a whole Stephen-King-Maximum-OverDrive meets Y2k situation on your hands. And that can't be good. Unless you're Stephen King and making Stephen King's money ($65 million a year!).
Well that's all the time we have for this bad boy. Come back soon as I continue talking about Date Functions in VBScript.
Till then...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |