|
-
Feb 21st, 2022, 05:44 AM
#1
Thread Starter
New Member
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.
-
Feb 21st, 2022, 06:14 AM
#2
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:
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:
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:
If (Date.Now - startTime) >= TimeSpan.FromHours(1) Then
'startTime is at least 1 hour before now.
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.
-
Feb 21st, 2022, 06:38 AM
#3
Thread Starter
New Member
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.
-
Feb 21st, 2022, 08:45 AM
#4
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|