473,516 Members | 3,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17447
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8046
by: Tiernan | last post by:
Hi all I'm looking for a way to find the number of weekdays between 2 dates In my form I have three fields for a begin date (dd)(mm)(yyyy) and three for the end date (dd)(mm)(yyyy) Now these values will be stored into a database and at the same time there will be an e-mail generated and send to me that contains the starting date en the end...
5
6715
by: SimonC | last post by:
Help needed for a Javascript beginner. As above in the subject... i need a javascript to run this, but not in the form of a web-page. I want to calculate it between 2 fields in a database that i have extracted into a report writer. Look forward to hearing.. Cheers... SimonC
3
29013
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the week . I've used the weekday() function to convert dates to numberic days of the week (1-7) I've used cdbl (date) to convert a date to a serial...
6
27935
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns to the right of these 2 fields to show the
10
12968
by: Brian Henry | last post by:
How would i take two dates startdate and enddate and subtract startdate from enddate to figure the number of days between the two? thanks
10
5363
by: Scott Kilbourn | last post by:
Hi, Does anyone know how to accurately calculate the number of days that have elapsed since 01/01/0000? I'd appreciate any help anyone could give me. Thanks
29
9031
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An example is: 36,525 represents 01/01/1900. The starting point date is considered to be : 00/00/0000. I have looked thru Help and used Google and...
9
21376
by: clintonb | last post by:
I'm looking for a way to calculate the number of days between two dates using standard C++ functions. Would it be as simple as just using the difftime() function and then dividing that result by the number of seconds in a day? - Clint
1
7188
by: bharathreddy | last post by:
This article will explain how we can get the count of weekdays in between two dates. This will be usefull if we want to count the number of working days between two dates. Example: ------------- USE GO --This function is used to count the number of weekdays between -- two dates. create function udf_Weekdays(@Weekday,@BeginDate...
0
7273
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7405
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3265
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.