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) | | | | re: date calculations
try this instead:
txtNumberOfDays.text =
datediff(days,dateTimeCheckout,dateTimeArrival).to string
--
Sent via .NET Newsgroups http://www.dotnetnewsgroups.com | | | | re: date calculations
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 ...[color=blue]
> "Charlie Brookhart" <charliebrookhart46@hotmail.com> wrote in
> news:JsydnQ-lC56jftPZnZ2dnUVZ_sCdnZ2d@adelphia.com:
>[color=green]
> > 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.[/color]
>
>
> 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).[/color] | | | | re: date calculations
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" <charliebrookhart46@hotmail.com> wrote in message
news:h62dnRLIetPjZdPZRVn-qQ@adelphia.com...[color=blue]
> 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 ...[color=green]
> > "Charlie Brookhart" <charliebrookhart46@hotmail.com> wrote in
> > news:JsydnQ-lC56jftPZnZ2dnUVZ_sCdnZ2d@adelphia.com:
> >[color=darkred]
> > > 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.[/color]
> >
> >
> > 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).[/color]
>
>[/color] | | | | re: date calculations
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 | | | | re: date calculations
Charlie Brookhart wrote:[color=blue]
> 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)
>
>[/color]
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() | | | | re: date calculations
Try this:
Dim ts As TimeSpan
ts = EndDate.Subtract(StartDate)
txtNumberOfDays.Text = ts.Days
There are other properties of the TimeSpan object as well | | | | re: date calculations
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" <guffa@guffa.com> wrote in message
news:e73b7hQaGHA.3992@TK2MSFTNGP05.phx.gbl...[color=blue]
> Charlie Brookhart wrote:[color=green]
> > I am creating a program that involves having to find the difference[/color][/color]
between[color=blue][color=green]
> > 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[/color][/color]
dates[color=blue][color=green]
> > that I have selected. I hope that someone here can help me figure out[/color][/color]
why it[color=blue][color=green]
> > 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)
> >
> >[/color]
>
> 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 =
>[/color]
dateTimeCheckout.Value.Date.Subtract(dateTimeArriv al.Value.Date).TotalDays.T
oString() | | | | re: date calculations
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" <charliebrookhart46@hotmail.com> schreef in bericht
news:Ba2dnfCoA46gFtLZnZ2dnUVZ_s-dnZ2d@adelphia.com...[color=blue]
> 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" <guffa@guffa.com> wrote in message
> news:e73b7hQaGHA.3992@TK2MSFTNGP05.phx.gbl...[color=green]
>> Charlie Brookhart wrote:[color=darkred]
>> > I am creating a program that involves having to find the difference[/color][/color]
> between[color=green][color=darkred]
>> > 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[/color][/color]
> dates[color=green][color=darkred]
>> > that I have selected. I hope that someone here can help me figure out[/color][/color]
> why it[color=green][color=darkred]
>> > 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)
>> >
>> >[/color]
>>
>> 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 =
>>[/color]
> dateTimeCheckout.Value.Date.Subtract(dateTimeArriv al.Value.Date).TotalDays.T
> oString()
>
>[/color] |  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|