65.9K
CodeProject is changing. Read more.
Home

VBScript IsTime Function

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (2 votes)

Nov 1, 2007

1 min read

viewsIcon

46440

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.

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