PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

datetimepicker finding the hours between two dates

 
 
SteveZmyname
Guest
Posts: n/a
 
      5th Apr 2010
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 With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      5th Apr 2010
Check out help on DATEDIFF() function

http://msdn.microsoft.com/en-us/library/b5xbyt6f(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.

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      5th Apr 2010
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" <(E-Mail Removed)> wrote in message
news40BF1C0-4941-45E2-B201-(E-Mail Removed)...
> 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 With Quote
 
SteveZmyname
Guest
Posts: n/a
 
      5th Apr 2010
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" <(E-Mail Removed)> wrote in message
> news40BF1C0-4941-45E2-B201-(E-Mail Removed)...
> > 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 With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      5th Apr 2010
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" <(E-Mail Removed)> wrote in message
news:8A18B17B-69B7-4F4E-90C3-(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news40BF1C0-4941-45E2-B201-(E-Mail Removed)...
>> > 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining work hours between dates / hours Andrew Microsoft Excel Worksheet Functions 3 30th Jul 2008 06:38 PM
DateTimePicker and visibility to Min/Max Dates. Chris Shepherd Microsoft C# .NET 12 21st Sep 2007 11:40 PM
bolded dates on datetimepicker Brian Henry Microsoft VB .NET 0 9th Dec 2004 09:11 PM
datetimepicker - set hours and minutes andreas Microsoft VB .NET 2 25th Jun 2004 04:47 PM
2nd Attempt: Null Dates in DateTimePicker Control Bruce Vander Werf Microsoft Dot NET Compact Framework 4 13th Dec 2003 05:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:59 AM.