date calculations

  • Thread starter Thread starter Charlie Brookhart
  • Start date Start date
C

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)
 
try this instead:

txtNumberOfDays.text =
datediff(days,dateTimeCheckout,dateTimeArrival).tostring
 
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).
 
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?
 
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)
 
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/d.../cpref/html/frlrfsystemtimespanclasstopic.asp

http://msdn.microsoft.com/library/d...ef/html/frlrfsystemtimespanclassdaystopic.asp

I hope this helps,

Cor
 
Charlie said:
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()
 
Try this:

Dim ts As TimeSpan

ts = EndDate.Subtract(StartDate)

txtNumberOfDays.Text = ts.Days

There are other properties of the TimeSpan object as well
 
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 said:
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()
 
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
 
Back
Top