Click here to Skip to main content
15,868,419 members
Articles / Web Development / ASP.NET
Article

VBScript IsTime Function

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
1 Nov 20071 min read 45.9K   9   2
Sample code for IsTime function similar to VBScript's IsDate

Introduction

It may exist, but my limited internet searching talent could not lead me to a VBScript IsTime function similar to the IsDate function provided by VBScript. So, using old technology, a book, I came up with the following example.

Please see Scott Barbour's comment below. As it turns out, IsDate will do this for you.

Background

The book I mentioned above is VBScript Programmer's Reference, 2nd edition. It's an excellent resource and worth the money for anyone who plans to write even a little VBScript.

Using the code

The code seems to work fairly well. However, I have not performed an exaustive unit test so use at your own peril.

The code accepts a time string str and first checks for an empty string. That check probably isn't necessary, but it was there from some previous attempts so I left it. Second, it uses TimeValue to check for a valid time. TimeValue will create a runtime error if the time string is invalid so I turn off the error control switch with On Error Resume Next. I turn the error control switch back on with On Error GoTo 0 when I'm done.

VBScript
function IsTime (str)
  if str = "" then
    IsTime = false
  else
    On Error Resume Next
    TimeValue(str)
    if Err.number = 0 then
      IsTime = true
    else
      IsTime = false
    end if
    On Error GoTo 0
  end if
end function

Points of Interest

You would think an experienced C++ programmer wouldn't have any trouble with VBScript, but not this guy. Oh well, diversity makes a nerds life interesting. Happy programming!

History

Submitted, 11/01/2007.
Edited, 11/05/2007

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIsDate() will do this Pin
Scott Barbour1-Nov-07 6:14
Scott Barbour1-Nov-07 6:14 
The Date datatype includes the time component, so if you pass a string containing only a time, the IsDate() function will return true if it is a valid time.
GeneralRe: IsDate() will do this Pin
sdkjfhsadlkufjhlawuhfp1-Nov-07 7:08
sdkjfhsadlkufjhlawuhfp1-Nov-07 7:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.