none
How get the difference of TIME RRS feed

  • Question

  •  

    I have a problem on how to get the difference of time (09:24:00 PM - 09:00:00 PM)?

     

    Is there any way to solve this? Can u help me please:(

     

    I am using Visual Basic 2005 for this program, how can i do this?

    Thursday, December 29, 2005 7:57 AM

Answers

  • You can use either the DateDiff function or the TimeSpan structure.  I've included a simple example of each:

            Dim d As Long

            d = DateDiff(DateInterval.Minute, #9:00:00 PM#, #9:24:00 PM#)

            MsgBox("The difference is " & d.ToString & " minutes.", , "DateDiff Function")

            Dim t As System.TimeSpan, dt1, dt2 As DateTime

            dt1 = #9:24:00 PM#

            dt2 = #9:00:00 PM#

            t = dt1.Subtract(dt2)

            MsgBox("The difference is " & t.ToString & ".", , "TimeSpan Structure")

    HTH,

    Dave

    Thursday, December 29, 2005 4:01 PM

All replies

  • You can use either the DateDiff function or the TimeSpan structure.  I've included a simple example of each:

            Dim d As Long

            d = DateDiff(DateInterval.Minute, #9:00:00 PM#, #9:24:00 PM#)

            MsgBox("The difference is " & d.ToString & " minutes.", , "DateDiff Function")

            Dim t As System.TimeSpan, dt1, dt2 As DateTime

            dt1 = #9:24:00 PM#

            dt2 = #9:00:00 PM#

            t = dt1.Subtract(dt2)

            MsgBox("The difference is " & t.ToString & ".", , "TimeSpan Structure")

    HTH,

    Dave

    Thursday, December 29, 2005 4:01 PM
  • Great...What is the best way of accomplishing this within a ASP.net form?  Additionally, the StartDate and EndDate may be on different days, what would be the best way of saving the StartDate and EndDate in a SQL DB (what datatype??) I was thinking of using the Calendar Control to choose the StartDate and then manually key in the StartTime and the same thing with the EndDate and EndTime. Would anybody have any better suggestions and possibly code snippets that would be a good start?  Thanks in Advanced
    Thursday, April 27, 2006 4:16 AM
  • With the right properties set, you can use the MonthCalendar control to get both Start and End time (if the two days are fairly close together).  Set MaxSelectionCount to the maximum number of days you want to allow selection for.  The Start date can be set to the SelectionRange.Start property, and the End date to SelectionRange.End property.

    If this isn't very good, use a DateTimePicker for Start date, and another for end date.  Then the user can bring up his own little calendars if he wants them, or work with the dates more directly.

    In any case, the start and end times should probably be normal Textboxes (though you might be able to work them into the DateTimePickers).  Use the IsDate() function to test to see if it's a valid date.  To combine start and end to a single datetime figure, convert the date part to a string using .ToShortDateString and the time part using .ToShortTimeString.  Concatenate these strings and put them in a single variable of type Date.  So we should have two Date variables representing start date/time and end date/time.

    If you're saving a DateTime in an SQL database, the 'datetime' type is best, because you can plug it right in with the contents of a Date variable.

    Thursday, April 27, 2006 7:30 PM