In our previous article we talked about loops and touched briefly upon creating your own functions. As you know, VBScript comes packed with its own pre-defined functions (like our buddy document.write), and in this article we are going to cover most, if not all, of the Date Functions. There are quite a few, so let's get started.
Contributed by James Payne Rating: / 7 January 07, 2008
The CDate function allows the user to check if a value can be converted to a date or time. It can also be used to do the actual conversion. Here is an example that converts a string to a date:
<html>
<body>
<script type="text/vbscript">
birthdate="April 22 1977"
if IsDate(birthdate) then
document.write(CDate(birthdate))
end if
</script>
</body>
</html>
The result of this program would be:
4/22/1977
Note that in the above example the valid date could be converted. But what if it couldn't? Try changing the code to show birthdate= "Afrail 10 2BD0." You will notice that there is no output; that is because the value cannot be converted to a date. Ideally, we would want to let the user know that the value is invalid. Here is how we would do so:
<html>
<body>
<script type="text/vbscript">
birthdate="Afrail 22 1977"
if IsDate(birthdate) then
document.write(CDate(birthdate))
else
document.write("That value cannot be converted")
document.write("<p>The pain train is coming. Whoo! Whoo!</p>")
end if
</script>
</body>
</html>
Because the value is invalid, and we added an else clause, this results in the text:
That value cannot be converted
The pain train is coming. Whoo! Whoo!
You can also convert numbers to a date, like so:
<html>
<body>
<script type="text/vbscript">
releasedate=#12/22/2007#
if IsDate(releasedate) then
document.write(CDate(releasedate))
end if
</script>
</body>
</html>
Or
<html>
<body>
<script type="text/vbscript">
releasedate=#12 22 2007#
if IsDate(releasedate) then
document.write(CDate(releasedate))
end if
</script>
</body>
</html>
And lastly, you can convert a value to time in the following manner:
<html>
<body>
<script type="text/vbscript">
witchinghour="12:00:00 AM"
if IsDate(witchinghour) then
document.write(CDate(witchinghour))
else
document.write("That isn't the witching hour you fool!")
If you want to print out the date according to your current system time, you can do so in this exciting manner:
<html>
<body>
<script type="text/vbscript">
document.write("The Date is: ")
document.write(Date)
</script>
</body>
</html>
We can also form calculations with the Date function:
<html>
<body>
<script type="text/vbscript">
document.write("The Date minus one day is: ")
document.write(Date -1 & "<p> </p>")
document.write("The Date minus a year is: ")
document.write(Date -365)
</script>
</body>
</html>
This results in:
The Date minus one day is: 12/3/2007
The Date minus a year is: 12/4/2006
Of course if we really want to perform math on dates, we should use the DateAdd function.
Adding Dates
To perform math on dates, it is best to use the DateAdd function. The syntax of the function is: DateAdd(interval,number,date). There are several parameters that you can work with:
yyyy: Used for year
q: Used for quarter
m: Used for month
y: Used for day of the year
d: Used for day
w: Used for weekday
ww: Used for week of year
h: Used for hour
n: used for minute
s: Used for second
Here are some examples:
Adding a month to today's date:
<html>
<body>
<script type="text/vbscript">
document.write("Today's date plus a month is: ")
document.write(DateAdd("m",1,date))
</script>
</body>
</html>
Adding Five Years to Today's Date:
<html>
<body>
<script type="text/vbscript">
document.write("Today's date plus five years is: ")
document.write(DateAdd("yyyy",5,date))
</script>
</body>
</html>
Adding Five Years to Any Date I Specify OR Making Myself Feel Old
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 ")
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.