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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to detect a certain character in a string?

Status
Not open for further replies.

SurvivorTiger

Programmer
Jul 9, 2002
265
US
Hi everyone,
i have a textbox where the user is supposed to enter a time, like 5:30. Now i need to do some calculations with the time and some other values, but instead of 5:30, the value that the calculations should be done with should be 5.5 (30 minutes = half an hour), now is there anyway to say if ":" is detected then the value after that ":" equals that value / 60 and then add that value to the value before the ":" which is 5 in this case...or is another way of doing this?
Thanks in advance

AIM: survivertiger & Ye This Is Me
 
Convert Date/Time to string in "hh:mm" format, do a string split (either split or mid + instr), divide val(minutes/60), add to hours and convert back to string.

Here's the mid + instr version:
[tt]
'---------------------------------------------------------------------------------------
' Procedure : fnDecHours
' DateTime : 13/07/2003 06:58
' Author : John M
' Purpose :
' Takes : Date/Time value
' Returns : Hours in decimal format
'---------------------------------------------------------------------------------------
'
Public Function fnDecHours(T As Date) As String
Dim strT As String, valT As Double
strT = Format(T, "hh:mm")
valT = Val(Format(T, "h")) + Val(Right$(strT, Len(strT) - InStr(1, strT, ":"))) / 60
fnDecHours = CStr(Round(valT, 2))
End Function
[/tt]
and the Split version
[tt]
'---------------------------------------------------------------------------------------
' Procedure : fnDecHours1
' DateTime : 13/07/2003 07:01
' Author : John M
' Purpose :
' Takes : Date/Time value
' Returns : Hours in decimal format
'---------------------------------------------------------------------------------------
'''
Public Function fnDecHours1(T As Date) As String
Dim strT() As String, valT As Double
strT = Split(Format(T, "hh:mm"), ":")
valT = Val(strT(0)) + Val(strT(1)) / 60
fnDecHours1 = CStr(Round(valT, 2))
End Function[/tt]

It's a pretty straighforward exercise - may be worth your while examining it - It's often easier to do it yourself when you see how it works

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
just a thought but the time date/value truncated and subed from the true value * 24

get the date/time into a value (datevar)

datenum = (datevar - int(datevar))*24

should give hours decimal without the day value

 

Another variation:

curTime = Hour(TheTime) + (Minute(TheTime)/60)
 
curTime = Hour(TheTime) + (Minute(TheTime)/60)

i think it would be better like this?

curTime = Hour(TheTime) & "." & (Minute(TheTime)/60)

 
Blankblank, That will not work.
You would then end up with something like:

TheTime = "10:30"
10.0.5

The Hour() and Minute() functions returns a number representing the Hour/Minute portion of the time.

So, we are adding the numbers together.
Because the Minute() function returns the minutes as a whole number, we determine what part of an hour this is by dividing by 60.

So,

Hour("10:30") returns 10
Minute("10:30") returns 30
Minute("10:30")/60 returns 0.5

10 + 0.5 = 10.5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top