Home All Groups Group Topic Archive Search About
Author
26 Apr 2006 3:04 AM
Charlie Brookhart
I am creating a program that involves having to find the difference between
two dates and converting it to a number to be used for calculations. The
problem is that the way it is setup, VB is not doing anything with the dates
that I have selected. I hope that someone here can help me figure out why it
is not working what I have to do to make it work.

This the code I have for the date calculations.

txtNumberOfDays.text = Val(dateTimeCheckout.Value.Date.ToString()) -
Val(dateTimeArrival.Value.Date.ToString)

Author
26 Apr 2006 3:45 AM
AMDRIT
try this instead:

txtNumberOfDays.text =
datediff(days,dateTimeCheckout,dateTimeArrival).tostring


--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Author
26 Apr 2006 4:25 AM
Spam Catcher
"Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in
news:JsydnQ-lC56jftPZnZ2dnUVZ_sCdnZ2d@adelphia.com:

> I am creating a program that involves having to find the difference
> between two dates and converting it to a number to be used for
> calculations. The problem is that the way it is setup, VB is not doing
> anything with the dates that I have selected. I hope that someone here
> can help me figure out why it is not working what I have to do to make
> it work.
>
> This the code I have for the date calculations.


You can also do:


EndDate.Substract(StartDate)

It'll return a TimeSpan object which has a variety of caluations (from
total days, total minutes, etc).
Author
26 Apr 2006 4:35 AM
Charlie Brookhart
So, if I use EndDate.Subtract(StartDate), then it would look like this:

txtNumberOfDays = EndDate.Subtract(StartDate) ? Would that return a whole
number value like 3 days or 5 days?

Show quoteHide quote
"Spam Catcher" <spamhoneypot@rogers.com> wrote in message
news:Xns97B1427E74A8usenethoneypotrogers@127.0.0.1...
> "Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in
> news:JsydnQ-lC56jftPZnZ2dnUVZ_sCdnZ2d@adelphia.com:
>
> > I am creating a program that involves having to find the difference
> > between two dates and converting it to a number to be used for
> > calculations. The problem is that the way it is setup, VB is not doing
> > anything with the dates that I have selected. I hope that someone here
> > can help me figure out why it is not working what I have to do to make
> > it work.
> >
> > This the code I have for the date calculations.
>
>
> You can also do:
>
>
> EndDate.Substract(StartDate)
>
> It'll return a TimeSpan object which has a variety of caluations (from
> total days, total minutes, etc).
Author
26 Apr 2006 4:48 AM
Charlie Brookhart
This method does not work because dateddiff and days are not declared.

txtNumberOfDays.text =
datediff(days,dateTimeCheckout,dateTimeArrival).tostring
_________________________________________

This method does not work because EndDate and StartDate are not declared.

EndDate.Subtract(StartDate)


Show quoteHide quote
"Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in message
news:h62dnRLIetPjZdPZRVn-qQ@adelphia.com...
> So, if I use EndDate.Subtract(StartDate), then it would look like this:
>
> txtNumberOfDays = EndDate.Subtract(StartDate) ? Would that return a whole
> number value like 3 days or 5 days?
>
> "Spam Catcher" <spamhoneypot@rogers.com> wrote in message
> news:Xns97B1427E74A8usenethoneypotrogers@127.0.0.1...
> > "Charlie Brookhart" <charliebrookhar***@hotmail.com> wrote in
> > news:JsydnQ-lC56jftPZnZ2dnUVZ_sCdnZ2d@adelphia.com:
> >
> > > I am creating a program that involves having to find the difference
> > > between two dates and converting it to a number to be used for
> > > calculations. The problem is that the way it is setup, VB is not doing
> > > anything with the dates that I have selected. I hope that someone here
> > > can help me figure out why it is not working what I have to do to make
> > > it work.
> > >
> > > This the code I have for the date calculations.
> >
> >
> > You can also do:
> >
> >
> > EndDate.Substract(StartDate)
> >
> > It'll return a TimeSpan object which has a variety of caluations (from
> > total days, total minutes, etc).
>
>
Author
26 Apr 2006 1:24 PM
Chris Dunaway
Try this:

Dim ts As TimeSpan

ts = EndDate.Subtract(StartDate)

txtNumberOfDays.Text = ts.Days

There are other properties of the TimeSpan object as well
Author
26 Apr 2006 5:20 AM
Cor Ligthert [MVP]
Charlie,

There are in VB endless methods to handles dates.
But no methods to calculate with strings as you do.

Your instruction is in fact like this.
txtNumberOfDays.text = Val("Apples" - "Pears")

You can look at the datediff method, but better to the timespan methods.

Be aware that you are comparing with this exact complete days is less than a
million, so you have to decide yourself if a day plus some milliseconds  is
one day or two.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtimespanclasstopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtimespanclassdaystopic.asp

I hope this helps,

Cor
Author
26 Apr 2006 8:07 AM
Göran_Andersson
Charlie Brookhart wrote:
> I am creating a program that involves having to find the difference between
> two dates and converting it to a number to be used for calculations. The
> problem is that the way it is setup, VB is not doing anything with the dates
> that I have selected. I hope that someone here can help me figure out why it
> is not working what I have to do to make it work.
>
> This the code I have for the date calculations.
>
> txtNumberOfDays.text = Val(dateTimeCheckout.Value.Date.ToString()) -
> Val(dateTimeArrival.Value.Date.ToString)
>
>

If it's not doing anything with the dates, your code is simply not
executed at all.

It has do be doing something, but I imagine nothing useful. You
shouldn't convert the dates to strings and back to numbers, as the
string representation of a date can not be interpreted as a single number.

You should use the methods of the DateTime class for comparing the
dates. Use the Subtract method and you will get a TimeSpan object, from
which you can get the difference between the dates most any way you
like. For an example:

txtNumberOfDays.text =
dateTimeCheckout.Value.Date.Subtract(dateTimeArrival.Value.Date).TotalDays.ToString()
Author
26 Apr 2006 3:01 PM
Charlie Brookhart
Goran:

Thanks for your suggestion. I will give it a try.

Cor:

You say that my instructions are the equivalent of txtNumberOfDays.text =
Val("Apples" - "Pears"). What I don't understand is why doesn't VB see the
instructions I have as a value that I am trying to get, especially when I
start out using Val and then more specifically indicate that I want to get a
date value and subtract that value from another date value?

Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:e73b7hQaGHA.3992@TK2MSFTNGP05.phx.gbl...
> Charlie Brookhart wrote:
> > I am creating a program that involves having to find the difference
between
> > two dates and converting it to a number to be used for calculations. The
> > problem is that the way it is setup, VB is not doing anything with the
dates
> > that I have selected. I hope that someone here can help me figure out
why it
> > is not working what I have to do to make it work.
> >
> > This the code I have for the date calculations.
> >
> > txtNumberOfDays.text = Val(dateTimeCheckout.Value.Date.ToString()) -
> > Val(dateTimeArrival.Value.Date.ToString)
> >
> >
>
> If it's not doing anything with the dates, your code is simply not
> executed at all.
>
> It has do be doing something, but I imagine nothing useful. You
> shouldn't convert the dates to strings and back to numbers, as the
> string representation of a date can not be interpreted as a single number.
>
> You should use the methods of the DateTime class for comparing the
> dates. Use the Subtract method and you will get a TimeSpan object, from
> which you can get the difference between the dates most any way you
> like. For an example:
>
> txtNumberOfDays.text =
>
dateTimeCheckout.Value.Date.Subtract(dateTimeArrival.Value.Date).TotalDays.T
oString()
Author
26 Apr 2006 4:06 PM
Cor Ligthert [MVP]
Carlie,

The Val method is rarely used.
It returns from a string an undefined value.

ToString is a method that is in any object. And everyhing in VBNet is an
object.

If it is overriden it can return a string of numbers. However from which the
value says not much. .

If you want to calculate with dates, than you have almost forever to use
dates in net. Even when you say
A = Cdate("01-01-2000")-Cdate("12-12-1999") you are calculating with dates.

The basic of your solution by the way is in the message from Chris Dunaway,

I hope this helps,

Cor



Show quoteHide quote
"Charlie Brookhart" <charliebrookhar***@hotmail.com> schreef in bericht
news:Ba2dnfCoA46gFtLZnZ2dnUVZ_s-dnZ2d@adelphia.com...
> Goran:
>
> Thanks for your suggestion. I will give it a try.
>
> Cor:
>
> You say that my instructions are the equivalent of txtNumberOfDays.text =
> Val("Apples" - "Pears"). What I don't understand is why doesn't VB see the
> instructions I have as a value that I am trying to get, especially when I
> start out using Val and then more specifically indicate that I want to get
> a
> date value and subtract that value from another date value?
>
> "Göran Andersson" <gu***@guffa.com> wrote in message
> news:e73b7hQaGHA.3992@TK2MSFTNGP05.phx.gbl...
>> Charlie Brookhart wrote:
>> > I am creating a program that involves having to find the difference
> between
>> > two dates and converting it to a number to be used for calculations.
>> > The
>> > problem is that the way it is setup, VB is not doing anything with the
> dates
>> > that I have selected. I hope that someone here can help me figure out
> why it
>> > is not working what I have to do to make it work.
>> >
>> > This the code I have for the date calculations.
>> >
>> > txtNumberOfDays.text = Val(dateTimeCheckout.Value.Date.ToString()) -
>> > Val(dateTimeArrival.Value.Date.ToString)
>> >
>> >
>>
>> If it's not doing anything with the dates, your code is simply not
>> executed at all.
>>
>> It has do be doing something, but I imagine nothing useful. You
>> shouldn't convert the dates to strings and back to numbers, as the
>> string representation of a date can not be interpreted as a single
>> number.
>>
>> You should use the methods of the DateTime class for comparing the
>> dates. Use the Subtract method and you will get a TimeSpan object, from
>> which you can get the difference between the dates most any way you
>> like. For an example:
>>
>> txtNumberOfDays.text =
>>
> dateTimeCheckout.Value.Date.Subtract(dateTimeArrival.Value.Date).TotalDays.T
> oString()
>
>