Results 1 to 4 of 4

Thread: Arithmetic with times

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Location
    Belfast
    Posts
    109

    Arithmetic with times

    Hi i have two times in the format mm.ss (minutes.seconds)

    I want to subtract 1 minute and thirty seconds from 30 minutes eg. x = 30.00 - 1.30

    If i did that normally the result would be 28.7

    I of course want the answer to be 28.30

    Are there any mathematical genii out there who can help me?

    Thanks in advance, John

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. Private Function TimeDiff(ByVal strTime1 As String, ByVal strTime2 As String) As String
    2.         Dim sMinutes As Short = Split(strTime1, ".")(0) - Split(strTime2, ".")(0)
    3.         Dim sSeconds As Short = Split(strTime1, ".")(1) - Split(strTime2, ".")(1)
    4.  
    5.         sSeconds = sMinutes * 60 + sSeconds ' Convert Value to seconds
    6.  
    7.         'now convert back to minutes.seconds
    8.  
    9.         sMinutes = (sSeconds - (sSeconds Mod 60)) / 60
    10.         sSeconds = (sSeconds Mod 60)
    11.  
    12.         Return sMinutes & "." & sSeconds
    13.  
    14.  
    15.     End Function
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Use the TimeSpan object to work with time calculations:
    VB Code:
    1. Dim ts As New TimeSpan(0, 30, 0) 'start with 30 min
    2.         MsgBox(ts.Subtract(New TimeSpan(0, 1, 30)).ToString) 'subtract 1 min 30 sec

    You can enter the time as a string too if you prefer, just use the parse shared method.
    VB Code:
    1. ts = TimeSpan.Parse("0:30:00")
    2.         MsgBox(ts.Subtract(TimeSpan.Parse("0:01:30")).ToString)

  4. #4
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    I am testing performance and comparing time concumprtions in Java and .net and for this I need to calculated in milliseconds. My timestamps are:

    time1 = Now.Hour & ":" & Now.Minute & ":" & Now.Second & ":" & Now.Millisecond

    when the calculation begins and

    time2 = Now.Hour & ":" & Now.Minute & ":" & Now.Second & ":" & Now.Millisecond

    How do I subtract the two times when I need milliseconds?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width