Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default datetimepicker finding the hours between two dates

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default datetimepicker finding the hours between two dates

Check out help on DATEDIFF() function

http://msdn.microsoft.com/en-us/libr...6f(VS.80).aspx

--
Jacob


"SteveZmyname" wrote:

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default datetimepicker finding the hours between two dates

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

--
Rick (MVP - Excel)



"SteveZmyname" wrote in message
...
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.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default datetimepicker finding the hours between two dates

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.


"Rick Rothstein" wrote:

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

--
Rick (MVP - Excel)



"SteveZmyname" wrote in message
...
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.


.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default datetimepicker finding the hours between two dates

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?

--
Rick (MVP - Excel)



"SteveZmyname" wrote in message
...
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.


"Rick Rothstein" wrote:

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

--
Rick (MVP - Excel)



"SteveZmyname" wrote in message
...
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.


.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DateTimePicker sarndt Excel Programming 2 December 13th 09 04:20 PM
DateTimePicker control problem Jac Tremblay[_4_] Excel Programming 0 December 13th 08 02:54 AM
Determining work hours between dates / hours Andrew Excel Worksheet Functions 3 July 30th 08 06:38 PM
dates to hours Champ Excel Discussion (Misc queries) 2 July 3rd 08 07:07 PM
DateTimePicker crazybass2 Excel Programming 3 January 11th 06 11:34 PM


All times are GMT +1. The time now is 08:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"