VBScript: Final Date Functions

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
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
April 01, 2008
Rate this Article:
MEH MEH++


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)

Here and Now

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.

Time is Always Against Us

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

Timer...It Has Its Eye On You

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:


<html>

<body>

<script type="text/vbscript">

document.write(TimeSerial(10,8,32)) & "<br />"

document.write(TimeSerial(22,18,59)) & "<br />"

document.write(TimeSerial(0,0,0)) & "<br />"

document.write(TimeSerial(12,0,0)) & "<br />"

document.write(TimeSerial(22-14,18+3,12*3)) & "<br />"

</script>

</body>

</html>

And as always, our result:

  10:08:32 AM
  10:18:59 PM
  12:00:00 AM
  12:00:00 PM
  8:21:36 AM

Note that this works off of military time, so 12, 0, 0 equals 12:00:00 PM and 0,0,0 equals 12:00:00 AM.

The TimeValue() Function

This function returns a time that you specify, ranging from 0:00:00 (aka 12:00:00 AM) to 23:59:59 (aka 11:59:59 PM). Here is an example:


<html>

<body>

<script type="text/vbscript">

document.write(TimeValue("10:15:25")) & "<br />"

document.write(TimeValue("10:15:25 PM")) & "<br />"

document.write(TimeValue("22:15:25")) & "<br />"

document.write(TimeValue(#10:15:25 PM#)) & "<br />"

document.write(TimeValue("10:15"))& "<br />"

</script>

</body>

</html>

Resulting in:

  10:15:25 AM
  10:15:25 PM
  10:15:25 PM
  10:15:25 PM
  10:15:00 AM

Weekday() and WeekdayName()

These two functions return the day of week, as a numeric value (1-7) and the weekday name, respectively:


<html>

<body>

<script type="text/vbscript">

document.write(Weekday(Date)) & "<br />"

document.write("Here we give weekday a value: ") &(Weekday(2))& "<br />"

document.write(WeekdayName(Weekday(Date))) & "<br />"

document.write(WeekdayName(Weekday(Date),true)) & "<br />"

document.write(WeekdayName(4))

</script>

</body>

</html>

And the final result:

  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!

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