VBScript: Final Date Functions - Here and Now (Page 2 of 5 )
As we said before, the Now() function returns the current date and time, based on your system. If you wanted to display the current time for your user, then this is the function for you.
<html>
<body>
<script type="text/vbscript">
document.write(Now) &"<br />"
</script>
</body>
</html>
The result is (it will be different for you, as the time will change):
1/29/2008 10:29:45 AM
You can also perform math based on the Now() function and, as we've seen in prior articles, parse data from it. Here, in this example, we will subtract days, months, and even years from the current date and time:
<html>
<body>
<script type="text/vbscript">
document.write("The date and time is: ")&(Now) & "<br /> <br />"
document.write("I was born on: ")&(Now-11239)& "<br /> <br />"
document.write("A year ago the date was: ") & (Now-365) & "<br /> <br />"
document.write("A month ago it was: ") & (Now-30) & "<br /> <br />"
document.write("In ten years it will be: ") & (Now+3650)
</script>
</body>
</html>
This code results in:
The date and time is: 1/29/2008 10:40:43 AM
I was born on: 4/22/1977 10:40:43 AM
A year ago the date was: 1/29/2007 10:40:43 AM
A month ago it was: 12/30/2007 10:40:43 AM
In ten years it will be: 1/26/2018 10:40:43 AM
And here is how you can parse out the various increments of time:
<html>
<body>
<script type="text/vbscript">
document.write("The week of the year is: ")&(DatePart("ww",Now)) & "<br />"
document.write("The day of the year is: ")&(DatePart("d",Now)) & "<br />"
document.write("The year is: ")&(DatePart("YYYY",Now)) & "<br />"
document.write("The weekday is: ")&(DatePart("w",Now)) & "<br />"
document.write("The hour is: ")&(DatePart("H",Now)) & "<br />"
document.write("The minute is: ")&(DatePart("n",Now)) & "<br />"
document.write("The second is: ")&(DatePart("s",Now)) & "<br />"
document.write("This is quarter: ")&(DatePart("q",Now))
</script>
</body>
</html>
Resulting in:
The week of the year is: 5
The day of the year is: 29
The year is: 2008
The weekday is: 3
The hour is: 10
The minute is: 52
The second is: 21
This is quarter: 1
Again, this will have different values when you try it, as the time will be different.
Next: Time is Always Against Us >>
More BrainDump Articles
More By James Payne