wildarmsdave
asked on
Turn current time into a percentage of 24 hours.
Hi,
What I have is a time in a textbox. The time can either be in 12 or 24 hr format (whichever is easier to work with.)
What I need to do is to turn this figure into a percentage of 24 hours...
So eg 12:00pm would be 50% (as we are half way through the day...) I need the percentage for hours AND minutes so 12:01pm would be 50. errrr something percent!!
Does anyone have any idea how this can be done?
Many thanks...
Dave.
What I have is a time in a textbox. The time can either be in 12 or 24 hr format (whichever is easier to work with.)
What I need to do is to turn this figure into a percentage of 24 hours...
So eg 12:00pm would be 50% (as we are half way through the day...) I need the percentage for hours AND minutes so 12:01pm would be 50. errrr something percent!!
Does anyone have any idea how this can be done?
Many thanks...
Dave.
Something like...
Option Explicit
Private Sub Command1_Click()
Dim p As String
p = TimePercentage(Text1.Text)
MsgBox p
End Sub
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(Text1.Text) Then
Dim dt As Date
dt = CDate(Text1.Text)
Dim pct As Double
pct = ((Hour(dt) * 60) + Minute(dt)) / 1440 * 100
TimePercentage = Format(pct, "0.00") & "%"
End If
End Function
Option Explicit
Private Sub Command1_Click()
Dim p As String
p = TimePercentage(Text1.Text)
MsgBox p
End Sub
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(Text1.Text) Then
Dim dt As Date
dt = CDate(Text1.Text)
Dim pct As Double
pct = ((Hour(dt) * 60) + Minute(dt)) / 1440 * 100
TimePercentage = Format(pct, "0.00") & "%"
End If
End Function
Ooops...I didnt' change the parameter in my function...should be:
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(tm) Then
Dim dt As Date
dt = CDate(tm)
Dim pct As Double
pct = ((Hour(dt) * 60) + Minute(dt)) / 1440 * 100
TimePercentage = Format(pct, "0.00") & "%"
End If
End Function
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(tm) Then
Dim dt As Date
dt = CDate(tm)
Dim pct As Double
pct = ((Hour(dt) * 60) + Minute(dt)) / 1440 * 100
TimePercentage = Format(pct, "0.00") & "%"
End If
End Function
Try this
This code snippet designed with the following assumptions
a) the available time is in 24Hrs format.If you want to change it as 12 Hrs format then use one more flag to determine am/pm.
b) The Hrs & Mts seperator is :, If it something different make suitable code changes according to that
c) the name of the text box is txtTime
d) Result is showned in 'lblPercentage'(Lable control)
Dim HrsMts as Integer, Hrs as Single, Mts as Byte
Dim strTemp() as String
Dim Result as Single
strTemp = Split(txtTime.Text,":")
Hrs = strTemp(0)
Mts = strTemp(1)
Hrs = Hrs + (Csng(Mts)/60)
Result = Hrs / Csng(24) * Csng(100)
lblPercentage.Caption = Format(Result,"#0.00%")
This code snippet designed with the following assumptions
a) the available time is in 24Hrs format.If you want to change it as 12 Hrs format then use one more flag to determine am/pm.
b) The Hrs & Mts seperator is :, If it something different make suitable code changes according to that
c) the name of the text box is txtTime
d) Result is showned in 'lblPercentage'(Lable control)
Dim HrsMts as Integer, Hrs as Single, Mts as Byte
Dim strTemp() as String
Dim Result as Single
strTemp = Split(txtTime.Text,":")
Hrs = strTemp(0)
Mts = strTemp(1)
Hrs = Hrs + (Csng(Mts)/60)
Result = Hrs / Csng(24) * Csng(100)
lblPercentage.Caption = Format(Result,"#0.00%")
I think jmundsack's code works great
8->
May be i.m wrong and i have not test yet
8->
May be i.m wrong and i have not test yet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
may be just
Pct = CDbl(cdate(text1.text))) * 100
Pct = CDbl(cdate(text1.text))) * 100
That works EDDYKT.
Though it won't work if the entry in the TextBox contains a FULL date such as "4/28/06 11:00 AM".
Whereas my approach doesn't care. If the entry in the textbox contains a valid date and/or date/time, it will correctly return the "percentage" that the entered date/time represents for that day.
If there is no time portion, then it becomes midnight for that day and 0.00% is returned.
Though it won't work if the entry in the TextBox contains a FULL date such as "4/28/06 11:00 AM".
Whereas my approach doesn't care. If the entry in the textbox contains a valid date and/or date/time, it will correctly return the "percentage" that the entered date/time represents for that day.
If there is no time portion, then it becomes midnight for that day and 0.00% is returned.
If it works, don't take my comment as answer
Take jmundsack's
Take jmundsack's
you can just modify to support with date
i.e.
d = CDate(Text1.Text) - CLng(CDate(Text1.Text))
If d < 0 Then d = 1 + d
Debug.Print CDbl(d) * 100
i.e.
d = CDate(Text1.Text) - CLng(CDate(Text1.Text))
If d < 0 Then d = 1 + d
Debug.Print CDbl(d) * 100
Fair enough...but IMHO, my code is alot easier to read and understand... =)
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(tm) Then
Dim dt As Date
dt = CDate(tm)
Dim pct As Double
pct = ((Hour(dt) * 60) + Minute(dt)) / 1440 * 100
TimePercentage = Format(pct, "0.00") & "%"
End If
End Function
instead of:
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(tm) Then
Dim dt As Date
dt = CDate(Text1.Text) - CLng(CDate(Text1.Text))
If dt < 0 Then
dt = 1 + dt
End If
TimePercentage = Format(CDbl(dt) * 100, "0.00") & "%"
End If
End Function
But again, that's just my opinion!...
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(tm) Then
Dim dt As Date
dt = CDate(tm)
Dim pct As Double
pct = ((Hour(dt) * 60) + Minute(dt)) / 1440 * 100
TimePercentage = Format(pct, "0.00") & "%"
End If
End Function
instead of:
Private Function TimePercentage(ByVal tm As String) As String
If IsDate(tm) Then
Dim dt As Date
dt = CDate(Text1.Text) - CLng(CDate(Text1.Text))
If dt < 0 Then
dt = 1 + dt
End If
TimePercentage = Format(CDbl(dt) * 100, "0.00") & "%"
End If
End Function
But again, that's just my opinion!...
I think the more sophisticated functions with better error control deserve the points.
Jon
Jon
the conversation tooks place between experts only. There is no interaction from author??????? Dear Author Please give feedback.
ASKER
Hey guys. Sorry I haven't replied in a while but I''ve been moving house and haven't had internet access. I'm going on holiday for a week now but will try these suggestions once I get back.
As usual, thanks to everyone that's replied. Your help and comments are appreciated.
As usual, thanks to everyone that's replied. Your help and comments are appreciated.
ASKER
Phew! I've finally had chance to try this. Many apologies for the delay, espesh' as you were all so quick in replying. I've awarded Idle Mind the points purley because his was the 2nd reply and the first version I could get to work. Many thanks to everyone else.
Dave.
Dave.
Nice decision
Pct = CDbl(Tm) * 100
HTH-Jon