• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

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.
0
wildarmsdave
Asked:
wildarmsdave
  • 5
  • 4
  • 3
  • +3
1 Solution
 
jmundsackCommented:
Convert the time to Double and then multiply by 100.

Pct = CDbl(Tm) * 100

HTH-Jon
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
pradapkumarCommented:
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%")
0
 
EDDYKTCommented:
I think jmundsack's code works great

8->

May be i.m wrong and i have not test yet
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I couldn't get jmundsacks code to work...maybe I missed something...    ?
0
 
EDDYKTCommented:
may be just

Pct = CDbl(cdate(text1.text))) * 100
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
 
EDDYKTCommented:
If it works, don't take my comment as answer

Take jmundsack's
0
 
EDDYKTCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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!...
0
 
jmundsackCommented:
I think the more sophisticated functions with better error control deserve the points.

Jon
0
 
Naveen SwamyCommented:
you will get some idea on this

http://nsm1.nsm.iup.edu/rwinstea/metricclock.shtm 
0
 
pradapkumarCommented:
the conversation tooks place between experts only. There is no interaction from author??????? Dear Author Please give feedback.
0
 
wildarmsdaveAuthor Commented:
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.
0
 
wildarmsdaveAuthor Commented:
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.
0
 
pradapkumarCommented:
Nice decision
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now