Number of days between 2 dates...

  • Thread starter Thread starter Scott
  • Start date Start date
S

Scott

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
 
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,

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
 
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
 
Scott said:
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(DateTimePicker1.Value).Days)
///
 
Scott said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top