How to find TimeDifference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NareshN
    New Member
    • Aug 2010
    • 45

    How to find TimeDifference

    Hi All,

    How to find TimeDifference between TimeIn and TimeOut. I should get 10:30.
    Code:
    string frmtimestring = 7:30
        string toTimeString = 18:00
      decimal frmTime = Convert.ToDecimal(frmtimestring.Replace(":", "."));
     
     decimal totime = Convert.ToDecimal(toTimeString.Replace(":", "."));
               if (frmTime > totime)
               {
                    totime = totime + 2400;
    
               }
      string TimeDifference = Convert.ToString(frmTime - totime).Replace("-", "");
    Last edited by Frinavale; Dec 6 '10, 02:58 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • aspdotnetuser
    New Member
    • Nov 2010
    • 22

    #2
    Try this...
    Code:
    Dim frmtimestring As TimeSpan = New TimeSpan("7", "30", "0")
            Dim toTimeString As TimeSpan = New TimeSpan("18", "0", "0")
    
            Dim diffTime As TimeSpan = toTimeString.Subtract(frmtimestring)
    
            MsgBox(diffTime.Hours.ToString + " : " + diffTime.Minutes.ToString + " : " + diffTime.Seconds.ToString)
    Last edited by Frinavale; Dec 6 '10, 02:58 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      You can't really use decimals or integer to calculate differences between times.

      It is best to subtract DateTime objects from each other. This will result in a TimeSpan.

      Check out my reply to this post for a quick easy way to do this.

      -Frinny

      Comment

      Working...