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
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
March 25, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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:

  • 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 samples:

Parsing out the Month, Day, and Year from the Current Date:


<html>

<body>

<script type="text/vbscript">

document.write("Today's date is: ") & Date & "<br /><br />"

document.write("This is month number ") & (DatePart("m",Date))

document.write("<br />This is day number ") & (DatePart("d",Date))

document.write("<br />The year is ") & (DatePart("yyyy",Date))

</script>

</body>

</html>

This code results in the text:

  Today's date is: 11/28/2007

  This is month number 11
  This is day number 28
  The year is 2007

Parsing out the Month, Day, and Year from a Specified Date:


<html>

<body>

<script type="text/vbscript">

document.write("My next birthday is 04/22/2007") & "<br /><br />"

document.write("This is month number ") & (DatePart("m","04/22/2007"))

document.write("<br />This is day number ") & (DatePart("d","04/22/2007"))

document.write("<br />The year is ") & (DatePart("yyyy","04/22/2007"))

</script>

</body>

</html>

As you can see, the only difference is the replacement of the Date function with our specified date.

Serial Dating

The DateSerial...well it's hard to explain exactly what it does, so I am just going to show you. The syntax is DateSerial(year,month,day). Here we go:


<html>

<body>

<script type="text/vbscript">

document.write("I was born on: ")

document.write(DateSerial(1977,4,22) & "<br /")

</script>

</body>

</html>

Which results in:

  I was born on: 4/22/1977

You can also do this:


<html>

<body>

<script type="text/vbscript">

document.write("I was born on: ")

document.write(DateSerial(2007-30,12-8,4+18) & "<br /")

</script>

</body>

</html>

This also results in:

  I was born on: 4/22/1977

Valuing Your Date

The DateValue returns a value that is a Date type. Here it is in action:


<html>

<body>

<script type="text/vbscript">

document.write(DateValue("28-Nov-07") & "<br />")

document.write(DateValue("28-Nov"))

</script>

</body>

</html>

The result:

  11/28/2007

  11/28/2007

You will note that if you leave the year portion out of the statement, it will use the systems year.

What Day is it Anyway?

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

Day, Month, Year, Hour, Minute, Second

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.

Here is the code in all its glory:


<html>

<body>

<script type="text/vbscript">

document.write(IsDate("October 31, 2008") & "<br />")

document.write(IsDate("I eat Biscuits Yum") & "<br />")

document.write(IsDate(#10/31/08#) & "<br />")

document.write(IsDate("12/13/2008"))

</script>

</body>

</html>

This returns the following results:

  True
  False
  True
  True

Figuring Out Which Month it is

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.

Till then...

blog comments powered by Disqus
BRAINDUMP ARTICLES

- Microsoft Windows 8 Committed to Cloud Compu...
- Independent Developers Favor Windows Phone 7
- Dell Introduces VMware-based Cloud
- Microsoft and Skype Agree to Acquisition Deal
- Transfer Contacts in Microsoft Outlook
- Zune`s Next Steps
- Safari Books Online Review
- Does Microsoft Get Touch Screens Now?
- Microsoft`s Record Quarterly Earnings Not En...
- Basic Operations and Registers in Assembly
- Assembly Coding within Visual C/C++ IDE
- New Microsoft Office Coming with a Twist
- Microsoft`s FUSE Labs Unveils Spindex Social...
- HP Slate with Windows 7: Dead or Alive?
- Windows Phone 7 Mobile OS to Rival Android a...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials