none
How to calculate total hours between any two dates using datetimepicker in vb.net RRS feed

  • Question

  • I am working on a car parking management system. In this while taking a car's entry the datetimepicker auto takes the system date and user can't choose the date on a form. While exiting the user searches the car number on another form and this time the previously saved date comes into the datetimepicker1 and the current system date comes in datetimepicker2 while exiting the car.

    Now I have to calculate the difference in hours between these datetimepickers. Note: It doesn't matters what the date is, it may be on a single day or next day or whatever dates. We have to just calculate the hours.. And user can't choose the date, it is from current system time..

    Please help!!


    • Edited by Sarthak S Wednesday, July 10, 2019 4:25 AM
    Wednesday, July 10, 2019 4:23 AM

All replies

  • Hi,

    try the code:

     Dim span As TimeSpan = DateTimePicker2.Value - DateTimePicker1.Value
            Dim spanhours As Integer = (span.Days) * 24 + span.Hours

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 4:35 AM
    Moderator
  • Try this too:

       Dim hours As Double = (DateTimePicker2.Value – DateTimePicker1.Value).TotalHours

    Then you can use Math.Ceiling, for example, to round it obtaining an integer number.

    Wednesday, July 10, 2019 5:33 AM