VBScript Date Functions - Working with the Date Function
(Page 3 of 4 )
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:
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
<html>
<body>
<script type="text/vbscript">
document.write(DateAdd("yyyy",30,"22-April-1977"))
</script>
</body>
</html>
Subtracting 20 Days from A Date
<html>
<body>
<script type="text/vbscript">
document.write(DateAdd("d",-20,"22-04-1977"))
</script>
</body>
</html>
Next: The Vainglorious DateDiff Function >>
More BrainDump Articles
More By James Payne