In our previous article we discussed more of the Date Functions, like IsDate, Hour, Minute, Month, and so forth. In this article we will conclude our discussion of the VBScript Date Functions, and if there's time and room, we'll throw in the Format Functions just for the heck of it.
Contributed by James Payne Rating: / 1 April 01, 2008
SEARCH ASP FREE
See Also:
TOOLS YOU CAN USE
advertisement
We'll pick up where we left off, with the Now() function, which returns the current system date and time. But first, one last peek at our Date Functions table:
Function Name
What It Does
CDate
Used to change the date and time expression to the variant subtype Date.
Date
Used to return the current date, based on your system date.
DateAdd
Used to return a date in which a time interval you specified has been added
DateDiff
Used to return the difference between two dates.
DatePart
Used to return a specific part of a date.
DateSerial
Used to return the date for a year you specify: year, month, and day.
DateValue
Used to return a date.
Day
Used to return a day of the month; returns a number between 1 and 31.
FormatDateTime
Used to return an expression. Formats it as a date or as a time.
Hour
Used to return the hour. Returns a number between 0 and 23.
IsDate
Used to check whether a value can be converted to a date. Returns a boolean value.
Minute
Used to return the minutes of an hour. Value range is between 0 and 59.
Month
Returns the month. Value returned is a number ranging from 1-12.
MonthName
Returns the name of a month you specifiy.
Now
Returns the date and time based on your system's date and time.
Second
Returns the seconds of the minute. A value between 0 and 59.
Time
Used to return the system time.
Timer
Used to return the amount of seconds since 12:00 am.
TimeSerial
Used to return the time for a specified hour, minute, and second.
TimeValue
Used to return a time.
Weekday
Used to return a day of the week. Value is a number between 1 and 7.
WeekDayName
Used to return the name of a day in the week.
Year
Used to return a year. Value is a number representing the year. (Duh)
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.
The Matrix (well the second part) is probably my favorite movie of all time, if not second favorite (behind Shamalyan's Sixth Sense). Unfortunately, my brother ruined the whole trilogy for me, by pointing out how everything Morpheus says is profound, or in that serious tone, with that know-it-all look on his face. Like imagine going to Denny's with that guy and ordering some breakfast:
Waitress: What would you like sugar?
Morpheus: The question isn't what would I like; it's what nutrients does my body require to function. But then, no matter what I eat, here in your world, in the real I will still be eating a mixture of amino acids.
Waitress: So the rooty tooty fresh and fruity?
Morpheus: Yeah. That sounds good...
And furthermore, women are always complaining about being fat. Wouldn't Trinity's residual self image have been chubby? And all the guys would have like really massive...feet. Anyway.
The Time() function does one thing and it does it well: it returns the current time based on your system. That being said, if the system time is wrong, then the time will be wrong also. Here it is in action:
<html>
<body>
<script type="text/vbscript">
document.write(Time)
</script>
</body>
</html>
The result?
11:08:34 AM
And again, you can manipulate and parse the increments of time in the following manner:
<html>
<body>
<script type="text/vbscript">
document.write("The current system time is: ") & (Time) & "<br />"
document.write("The hour is: ") & (DatePart("h",Time)) & "<br />"
document.write("The minute is: ") & (DatePart("n",Time)) & "<br />"
document.write("The second is: ") & (DatePart("s",Time)) & "<br /> <br />"
</script>
</body>
</html>
Here is the outcome:
The current system time is: 11:14:33 AM The hour is: 11 The minute is: 14 The second is: 33
The Timer() function returns the amount of seconds since 12:00 am (midnight). You can also perform calculations based on the Timer() function, such as seeing how many hours or minutes have passed. Here it is in code:
<html>
<body>
<script type="text/vbscript">
document.write("The amount of seconds since 12 am: ") & (Timer) & "<br />"
document.write("The amount of minutes since 12 am: ") & (Timer/60) & "<br />"
document.write("The amount of hours since 12 am: ") & (Timer/3600) & "<br />"
</script>
</body>
</html>
Which gives us the result:
The amount of seconds since 12 am: 40718.42 The amount of minutes since 12 am: 678.6404 The amount of hours since 12 am: 11.31067
Cereal Time
I just scarfed down a box of Apple Jacks, probably the best cereal on the planet. I don't know why Cookie Crisp doesn't have the foresight to make those mini boxes of cereal. I mean they would have total market domination if they did, just from me alone.
The TimeSerial() function returns the time for an hour, minute, and second that you specify. It is comprised of three sections, separated by commas. The first is for the hour, the second is for minutes, and the third is for seconds. You can also perform math on these sections, as you will see below:
3 Here we give weekday a value: 2 Tuesday Tue Wednesday
Ok, so I lied. We didn't get to the Format() functions after all. We will however get to them in a future article...maybe even the next. So check back often...or else!