Results 1 to 4 of 4

Thread: Calculate time difference

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Question Calculate time difference

    Hi guys,

    Need a little help with a project of mine where I need to calculate the hour difference between two datetime values.

    The issue I have is subtracting past midnight time like: (00:00:00 - 23:00:00) = 60 minutes (1 hour)

    The idea behind is that I have to check a Datagridview list items if from the current time (=Now) if an hour has passed and then remove it.

    My code works ok until right before the midnight change.

    Code:
     For Each row As DataGridViewRow In dgvWashing.Rows
    
                        Dim StartDate, StartTime As DateTime
                        Dim StopDate, StopTime As DateTime
    
    
                        StartDate = Convert.ToDateTime((row.Cells(6).Value)).ToString("dd.MM.yyyy")
                        StartTime = Convert.ToDateTime((row.Cells(6).Value)).ToString("HH:mm:ss")
    
    
                        StopDate = DateTime.Now.ToString("dd.MM.yyyy")
                        StopTime = DateTime.Now.ToString("HH:mm:ss")
    
                        Dim seco = (StopTime - StartTime).TotalSeconds
    
                        Dim days As Integer = (StopDate.Date - StartDate.Date).Days
    
                        Dim rowID = row.Cells(0).Value.ToString
    Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calculate time difference

    You're making life difficult for yourself by treating dates and times as text. DO NOT convert between DateTime and String unless you need to convert text input to a DateTime or convert a DateTime for display or serialisation. DO NOT do so for calculations. This code is just crazy:
    vb.net Code:
    1. StartDate = Convert.ToDateTime((row.Cells(6).Value)).ToString("dd.MM.yyyy")
    You start by converting something to a DateTime. What is it that you're converting? If the grid already contains DateTimes then there's nothing to convert, so you should be casting. If it's text then a conversion is appropriate but then you go off the rails and convert that DateTime to a String and then back to a DateTime again. That's madness.

    If you already have a DateTime in the grid then just cast it:
    vb.net Code:
    1. Dim startTime = CDate(row.Cells(6).Value)
    otherwise the conversion is fine. You now have a DateTime that you can perform a calculation on:
    vb.net Code:
    1. If (Date.Now - startTime) >= TimeSpan.FromHours(1) Then
    2.     'startTime is at least 1 hour before now.
    3. End If
    There is absolutely no need for you to care about whether midnight falls between the two times. The whole point of a DateTime is that it contains date and time, so that is taken into account in the calculation.
    Last edited by jmcilhinney; Feb 21st, 2022 at 06:17 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Calculate time difference

    Yeah, now I feel pretty stupid. When you put it like that it makes perfect sense. Thank you very much for the detailed explanation. Every day you learn something, even if its not new, you see a different way of solving a simple issue. Thank you.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Calculate time difference

    A lot of people make that same mistake. I think it's because they are used to reading dates and times as text and so they think of them that way instead of the numbers that they really are and that they're treated as in software.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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