VBScript Date Functions

In our previous article we talked about loops and touched briefly upon creating your own functions. As you know, VBScript comes packed with its own pre-defined functions (like our buddy document.write), and in this article we are going to cover most, if not all, of the Date Functions. There are quite a few, so let's get started.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
January 07, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Before we dive in, here is a table showing the various Date Functions:

 

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. Returns 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)

Functioning it Up with CDate

The CDate function allows the user to check if a value can be converted to a date or time. It can also be used to do the actual conversion. Here is an example that converts a string to a date:


<html>

<body>

<script type="text/vbscript">

birthdate="April 22 1977"

if IsDate(birthdate) then

document.write(CDate(birthdate))

end if

</script>

</body>

</html>

The result of this program would be:

  4/22/1977

Note that in the above example the valid date could be converted. But what if it couldn't? Try changing the code to show birthdate= "Afrail 10 2BD0." You will notice that there is no output; that is because the value cannot be converted to a date. Ideally, we would want to let the user know that the value is invalid. Here is how we would do so:


<html>

<body>


<script type="text/vbscript">

birthdate="Afrail 22 1977"

if IsDate(birthdate) then

document.write(CDate(birthdate))

else

document.write("That value cannot be converted")

document.write("<p>The pain train is coming. Whoo! Whoo!</p>")

end if

</script>

</body>

</html>

Because the value is invalid, and we added an else clause, this results in the text:

  That value cannot be converted

  The pain train is coming. Whoo! Whoo!

You can also convert numbers to a date, like so:


<html>

<body>

<script type="text/vbscript">

releasedate=#12/22/2007#

if IsDate(releasedate) then

document.write(CDate(releasedate))

end if

</script>

</body>

</html>

Or


<html>

<body>

<script type="text/vbscript">

releasedate=#12 22 2007#

if IsDate(releasedate) then

document.write(CDate(releasedate))

end if

</script>

</body>

</html>

And lastly, you can convert a value to time in the following manner:


<html>

<body>

<script type="text/vbscript">

witchinghour="12:00:00 AM"

if IsDate(witchinghour) then

document.write(CDate(witchinghour))

else

document.write("That isn't the witching hour you fool!")

end if

</script>

</body>

</html>

Working with the Date Function

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:

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

The Vainglorious DateDiff Function

In the following example I created a program to tell the amount of years, months, minutes, and seconds since my birth:


<html>

<body>

<script type="text/vbscript">

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

document.write("<br />The amount of time since my magnanimous birth can be measured in the following ways: <br /><br />")

document.write(DateDiff("yyyy",Date,"04/22/1977")& (" years") & "<br />")

document.write(DateDiff("m",Date,"04/22/1977")& (" months") & "<br />")

document.write(DateDiff("n",Date,"04/22/1977")& (" minutes") & "<br />")

document.write(DateDiff("s",Date,"04/22/1977")& (" seconds"))

</script>

</body>

</html>

The result is:

  Today's date is: 12/4/2007

  The amount of time since my magnanimous birth can be measured in the following ways:

  -30 years
  -368 months
  -16103520 minutes
  -966211200 seconds

In the above example we calculated how much time had passed by subtracting my birthdate from the current time (well, based on our system timer). But what if I wanted to subtract a specified date from another specified date? I could do this:


<html>

<body>

<script type="text/vbscript">

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

document.write("If Jesus had lived to be a hundred ")

document.write("the difference in the date of his hundredth ")

document.write("birthday and my birthdate could be ")

document.write("measured thusly:<br /><br/>")

document.write(DateDiff("yyyy","12/25/100","04/25/1977")& (" years") & "<br />")

document.write(DateDiff("m","12/25/100","04/22/1977")& (" months") & "<br />")

document.write(DateDiff("n","12/25/100","04/22/1977")& (" minutes") & "<br />")

document.write(DateDiff("s","12/25/100","04/22/1977")& (" seconds"))

</script>

</body>

</html>

This results in the following:

  Today's date is: 12/4/2007
  If Jesus had lived to be a hundred the difference in the date of his hundredth birthday and my birthdate could be measured thusly:

  1877 years
  22516 months
  986850720 minutes
  59211043200 seconds

You will note that I used the year 100. My original intention was to use the date 12/25/0000 (I know Jesus wasn't REALLY born on that day) but if you use anything less than 100 for the year portion of the date, computers get confused and think you are referencing the 1900s. Crazy computers. Then the next thing you know you got yourself a whole Stephen-King-Maximum-OverDrive meets Y2k situation on your hands. And that can't be good. Unless you're Stephen King and making Stephen King's money ($65 million a year!).

Well that's all the time we have for this bad boy. Come back soon as I continue talking about Date Functions in VBScript.

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 4 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials