datetimepicker finding the hours between two dates

  • Thread starter Thread starter SteveZmyname
  • Start date Start date
S

SteveZmyname

Hello
Just like the subject says I am using two datetimepickers a start and end
and would like to know the hours between the two (or days). So far I have

Private Sub calcHours()
Dim totalHours As Integer
'totalHours = DateInitiatedDateTimePicker.Value.Day -
DateFinishedDateTimePicker.Value.Day
Dim startDate As DateTime = DateInitiatedDateTimePicker.Value
Dim finishDate As DateTime = DateFinishedDateTimePicker.Value
totalHours = startDate.Day + finishDate.Day
'totalHours = DateFinishedDateTimePicker.Value.Day -
DateInitiatedDateTimePicker.Value.Day
HoursTextBox.Text = totalHours
MessageBox.Show(totalHours)
End Sub

Not sure whether to add or subtract the dates. If I subtract then I always
get a 0.
If I add using the dates 1/15/2010 for start and 2/2/2010 for end I get a
value of 10.
This doesn't make sense to me for either hours or days.
 
What you posted is not correct VBA syntax, so I'm not sure what language are
you writing those in. Here is how you would do it in VB...

Private Sub CalculateHours()
Dim TotalHours As Long
Dim StartDate As Date
Dim FinishDate As Date
StartDate = DateInitiatedDateTimePicker.Value
FinishDate = DateFinishedDateTimePicker.Value
TotalHours = 24 * (FinishDate - StartDate)
End Sub
 
thanks for both responses.
I think this code is very close but it seems to be squawking at the *. It says
"Operator '*' is not defined for types integer and system.Timespan"
I added an Include System.Timespan but it still errors the same.
 
What language are you writing this in? In VB (and on Excel worksheets), Date
values are stored as floating point numbers with the whole number part
representing the number of days from "date zero" (so "date one" is
12/31/1899 in VB and it is 1/1/1900 in Excel worksheets... these "catch up"
with each other in 1904) and the floating point part is representing the
fraction of a day past midnight. So, in VB (and on Excel worksheets), you
can just subtract the value to get the number of days and fractions of a
day, so multiplying that difference by 24 (the number of hours in a day)
will, when assigned to a variable declared as Integer or Long, give you the
number of hours between the dates. As I said, this works in VB macros (and
on Excel worksheets), but I have no idea what programming language you are
currently using. Are you sure you are asking your question in the correct
newsgroup?
 
Back
Top