Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I be sure that a string is a valid time of day and NOT a date

Status
Not open for further replies.

DoniFromMars

Programmer
Aug 10, 2006
23
0
0
US
How can I be sure that a string is a valid time of day and NOT a date. Help!
 
Perhaps:

IsDate(Date & " " & strTime)
 
How do you define a time and a date? Dates without times can be validated like:
[DateTimeField]=DateValue([DateTimeField])
A datetime field that doesn't include any date part would be less than 1.

Duane MS Access MVP
Now help me support United Cerebral Palsy
 
I took Remous idea and wrapped it into a function
Code:
Public Function isTime(strTime As String) As Boolean
  isTime = IsDate(Date & " " & strTime)
End Function

Some tests
?isTime("#12:00#") & vbcrlf

1:67 am
False

36:00
False

12:00AM
True

-12:00 AM
False

12:00 AM
True

 
The point of my suggestion is that

"01/02/2008 12:45"

Will return false even though it is clearly a date, whereas

"12:45"

Will return true.

The additon of date to a correct date/time string invalidates it as a date, whereas adding date to a correct time does not.
 
I'm not sure what the OP is after. Dates and times both are stored as numbers. There are whole numbers (integers) and there are numbers that contain decimal values.

All dates also store the time. The time might be midnight which means the date value is a whole number. All times include a date. If the date part is Dec 30, 1899 then the numeric value is less than 1.

Duane MS Access MVP
Now help me support United Cerebral Palsy
 
I read this to mean that he had a string to start with and it was supposed to represent a time. So I think the OP needs to explain what the input is.

I assumed that the user wanted true if a string looked something like

00:01 to 23:59
or 12:00 AM to 11:59 PM

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top