469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Number of days between 2 dates...

I want to work out the difference (number of days) between 2 dates selected
using the date picker in vb.net and displaying this in textbox1 (which will
be un-editable to the user)

An MVP game me the following code, which looks acceptable to me in practice,
but appears to always through up 0 in textbox1??? Am I doing anything
blatantly wrong or is there anything else I need to do???

Dim d1, d2 As Date
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)

Is this code correct or do I need something else?

Scott
Nov 21 '05 #1
6 17261
Scott,

You need to put some date values into the 2 date variables. For example:

Dim d1, d2 As Date

d1 = #2/20/2005#
d2 = Now

MsgBox(CStr(d2.Subtract(d1).Days))

Kerry Moorman

"Scott" wrote:
I want to work out the difference (number of days) between 2 dates selected
using the date picker in vb.net and displaying this in textbox1 (which will
be un-editable to the user)

An MVP game me the following code, which looks acceptable to me in practice,
but appears to always through up 0 in textbox1??? Am I doing anything
blatantly wrong or is there anything else I need to do???

Dim d1, d2 As Date
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)

Is this code correct or do I need something else?

Scott

Nov 21 '05 #2
Scott,

I think it is correct although I would probably do this when you don't want
the days in elapsed hours/minutes/seconds.

CStr(d2.Date.Subtract(d1.Date).Days)

I hope this helps

Cor
Nov 21 '05 #3
Scott,
As Kerry suggests:
Dim d1, d2 As Date d1 = Me.DateTimePicker1.Value
d2 = Me.DateTimePicker2.Value Me.txtlength.Text = CStr(d2.Subtract(d1).Days)
Hope this helps
Jay

"Scott" <ho*******@virgin.net> wrote in message
news:mI****************@newsfe5-gui.ntli.net...I want to work out the difference (number of days) between 2 dates selected
using the date picker in vb.net and displaying this in textbox1 (which will
be un-editable to the user)

An MVP game me the following code, which looks acceptable to me in
practice, but appears to always through up 0 in textbox1??? Am I doing
anything blatantly wrong or is there anything else I need to do???

Dim d1, d2 As Date
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)

Is this code correct or do I need something else?

Scott

Nov 21 '05 #4
yeah that's what I was after...

I didn't know how to do the Me.DateTimePicker1.Value as I couldn't find
anything in the help files about that... But that good!!!

Cheers

Scott

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:el****************@TK2MSFTNGP10.phx.gbl...
Scott,
As Kerry suggests:
Dim d1, d2 As Date

d1 = Me.DateTimePicker1.Value
d2 = Me.DateTimePicker2.Value
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)


Hope this helps
Jay

"Scott" <ho*******@virgin.net> wrote in message
news:mI****************@newsfe5-gui.ntli.net...
I want to work out the difference (number of days) between 2 dates
selected using the date picker in vb.net and displaying this in textbox1
(which will be un-editable to the user)

An MVP game me the following code, which looks acceptable to me in
practice, but appears to always through up 0 in textbox1??? Am I doing
anything blatantly wrong or is there anything else I need to do???

Dim d1, d2 As Date
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)

Is this code correct or do I need something else?

Scott


Nov 21 '05 #5
"Scott" <ho*******@virgin.net> schrieb:
I want to work out the difference (number of days) between 2 dates selected
using the date picker in vb.net and displaying this in textbox1 (which will
be un-editable to the user)

An MVP game me the following code, which looks acceptable to me in
practice, but appears to always through up 0 in textbox1??? Am I doing
anything blatantly wrong or is there anything else I need to do???

Dim d1, d2 As Date
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)


You are never assigning a date to 'd1' and 'd2'. The code below will fill
'd1' and 'd2' with the dates selected in the datetimepicker controls:

\\\
Dim d1 As Date = DateTimePicker1.Value
Dim d2 As Date = DateTimePicker2.Value
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)
///

Alternatively you can use this (shorter) code:

\\\
Me.txtlength.Text = _
CStr(DateTimePicker2.Value.Subtract(DateTimePicker 1.Value).Days)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
Scott wrote:
I want to work out the difference (number of days) between 2 dates selected
using the date picker in vb.net and displaying this in textbox1 (which will
be un-editable to the user)

An MVP game me the following code, which looks acceptable to me in practice,
but appears to always through up 0 in textbox1??? Am I doing anything
blatantly wrong or is there anything else I need to do???

Dim d1, d2 As Date
Me.txtlength.Text = CStr(d2.Subtract(d1).Days)

Is this code correct or do I need something else?

Scott

Just after your declarations, you'll need lines like the following:

d1 = datetimepicker1.Value
d2 = datetimepicker2.Value

....but replace "datetimepicker1" and "datetimepicker2" with the names of
your date time pickers
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tiernan | last post: by
3 posts views Thread by jerry.ranch | last post: by
10 posts views Thread by Scott Kilbourn | last post: by
29 posts views Thread by james | last post: by
9 posts views Thread by clintonb | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.