469,950 Members | 2,333 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

date calculations

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)
Apr 26 '06 #1
8 1874
try this instead:

txtNumberOfDays.text =
datediff(days,dateTimeCheckout,dateTimeArrival).to string
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Apr 26 '06 #2
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" <sp**********@rogers.com> wrote in message
news:Xn*********************************@127.0.0.1 ...
"Charlie Brookhart" <ch****************@hotmail.com> wrote in
news:Js******************************@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).

Apr 26 '06 #3
This method does not work because dateddiff and days are not declared.

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

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

EndDate.Subtract(StartDate)
"Charlie Brookhart" <ch****************@hotmail.com> wrote in message
news:h6********************@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" <sp**********@rogers.com> wrote in message
news:Xn*********************************@127.0.0.1 ...
"Charlie Brookhart" <ch****************@hotmail.com> wrote in
news:Js******************************@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).


Apr 26 '06 #4
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/de...classtopic.asp

http://msdn.microsoft.com/library/de...sdaystopic.asp

I hope this helps,

Cor
Apr 26 '06 #5
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(dateTimeArriv al.Value.Date).TotalDays.ToString()
Apr 26 '06 #6
Try this:

Dim ts As TimeSpan

ts = EndDate.Subtract(StartDate)

txtNumberOfDays.Text = ts.Days

There are other properties of the TimeSpan object as well

Apr 26 '06 #7
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:e7**************@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(dateTimeArriv al.Value.Date).TotalDays.T
oString()
Apr 26 '06 #8
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

"Charlie Brookhart" <ch****************@hotmail.com> schreef in bericht
news:Ba******************************@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:e7**************@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(dateTimeArriv al.Value.Date).TotalDays.T
oString()

Apr 26 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Marc | last post: by
2 posts views Thread by androtech | last post: by
30 posts views Thread by Dr John Stockton | last post: by
11 posts views Thread by lduperval | last post: by
5 posts views Thread by Simon Dean | last post: by
3 posts views Thread by Glencannon4424 via AccessMonster.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.