HomeBrainDump VBScript: More Fun with the Date Functions
VBScript: More Fun with the Date Functions
In our first article on VBScript date functions, way back in January, we covered CDate, Date, DateAdd, and DateDiff. In this article, we'll be picking up where we left off. We still have a lot of ground to cover; who knew dates were so important? So let's get started.
Contributed by James Payne Rating: / 2 March 25, 2008
If you happened to miss the wise words of my first VBScript article on dates, never fear, you can review it right here. If you'd rather not, that's okay too.
We left off discussing the difference between my birth and Jesus's hypothetical hundredth birthday using the DateDiff function. Here we will carry on beginning with a little number I like to call the DatePart...
But first, our lovely 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 if 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)
Partying with the DatePart Function
DatePart allows you to extract a part of a date. Note that like most date functions, you can use the following parameters:
If we want to know which day of the month it is (represented by a value ranging from 1-31), we can use the Day() function, like so:
<html>
<body>
<script type="text/vbscript">
document.write("Today's date is: ") & Date & ("<br />")
document.write("The day of the month is ") & Day(Date)
</script>
</body>
</html>
This results in the printout:
Today's date is: 12/28/2008 The day of the month is 28
Formatting the Date/Time with the...(drum roll please) FormatDateTime() Function
If you want to specify how your date/time looks, one way to do so is with the FormatDateTime() function. There are five different formats you can use, starting with 0 and working through to number 4. Here they are in code:
<html>
<body>
<script type="text/vbscript">
document.write("Today's date is: ")
document.write(FormatDateTime(Date(),4))
document.write("<br />")
document.write("Today's date is: ")
document.write(FormatDateTime(Date(),3))
document.write("<br />")
document.write("Today's date is: ")
document.write(FormatDateTime(Date(),2))
document.write("<br />")
document.write("Today's date is: ")
document.write(FormatDateTime(Date(),1))
document.write("<br />")
document.write("Today's date is: ")
document.write(FormatDateTime(Date()))
document.write("<br />")
</script>
</body>
</html>
And the result:
Today's date is: 00:00 Today's date is: 12:00:00 AM Today's date is: 1/28/2008 Today's date is: Monday, January 28, 2008 Today's date is: 1/28/2008
The following script displays the Day, Month, Year, Hour, Minutes, and Second using each unit's respective function:
<html>
<body>
<script type="text/vbscript">
document.write("The Month's number is: ")& (Month(Date))
document.write("<br />")
document.write("The Day is: ")& (Day(Date))
document.write("<br />")
document.write("The Year is: ")& (Year(Date))
document.write("<br />")
document.write("The hour is: ")& (Hour(Time))
document.write("<br />")
document.write("The minute is: ")& (Minute(Time))
document.write("<br />")
document.write("The second is: ")& (Second(Time))
</script>
</body>
</html>
The result of this code follows:
The Month's number is: 12 The Day is: 28 The Year is: 2007 The hour is: 8 The minute is: 48 The second is: 42
The IsDate() Function
The IsDate() function is used to determine whether a particular expression can be converted into a date. Since the function returns a Boolean result, if the value can indeed be converted, a TRUE result is returned. If it cannot, then a FALSE is returned.
If you want to print out the name of a month (as opposed to the number, as with the Month() function), you can use the MonthName() function to do so. You can either return the full name or an abbreviated name by adding true to your code. Here is an example of how to use both methods:
<html>
<body>
<script type="text/vbscript">
document.write(MonthName(1))& "<br />"
document.write(MonthName(2))& "<br />"
document.write(MonthName(3))& "<br />"
document.write(MonthName(4))& "<br />"
document.write(MonthName(5))& "<br />"
document.write(MonthName(6))& "<br />"
document.write(MonthName(7))& "<br />"
document.write(MonthName(8))& "<br />"
document.write(MonthName(9))& "<br />"
document.write(MonthName(10))& "<br />"
document.write(MonthName(11))& "<br />"
document.write(MonthName(12))& "<br />"
document.write(MonthName(1,True))& "<br />"
document.write(MonthName(2,True))& "<br />"
document.write(MonthName(3,True))& "<br />"
document.write(MonthName(4,True))& "<br />"
document.write(MonthName(5,True))& "<br />"
document.write(MonthName(6,True))& "<br />"
document.write(MonthName(7,True))& "<br />"
document.write(MonthName(8,True))& "<br />"
document.write(MonthName(9,True))& "<br />"
document.write(MonthName(10,True))& "<br />"
document.write(MonthName(11,True))& "<br />"
document.write(MonthName(12,True))& "<br />"
</script>
</body>
</html>
And here is the result:
January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Well that's it for this article. Be sure to join us next time, when we pick up where we left off and work our way through the rest of the VBScript Date functions.