Strange date time error

  • Thread starter Thread starter Big Tony
  • Start date Start date
B

Big Tony

I have a piece of code written in VS.NET 2003. It runs fine on one machine and does not on another. It throws an invalid cast
exception. Any idea why there is a difference between machines?

Dim CurDate As Date = Now
Dim SpecTime As Date = "#07:08:30 PM#"

Eception thrown here:
CurDate = SpecTime + CurDate
 
Big Tony said:
I have a piece of code written in VS.NET 2003. It runs fine on one
machine and does not on another. It throws an invalid cast exception.
Any idea why there is a difference between machines?

Dim CurDate As Date = Now
Dim SpecTime As Date = "#07:08:30 PM#"

Eception thrown here:
CurDate = SpecTime + CurDate

The code does not work here. "#07:08:30 PM" is a string whereas SpecTime is
a Date. Please enable Option Strict.
 
I don't know about vs 2003 but you really just can't add dates any more..
use the
date.addseconds or minutes or hours method...

As to why the difference between machines ... It might be that the datetime
format settings on the machine are set different.

Big Tony said:
I have a piece of code written in VS.NET 2003. It runs fine on one machine
and does not on another. It throws an invalid cast
 
The expression doesn't really make sense. You can add a date interval to
another date, but you can't add an absolute date to another date. It really
doesn't evaluate to anything meaningful. For example, you can add X number
of days or X number of minutes or X number of hours to the current
date/time Now. Below you can add 7 hours 8 minutes 30 seconds, which is an
interval but 7:08:30PM is not a time INTERVAL itself. The other thing you
can do is get the amount of time that has elapsed between two date
expressions, like how man hours have passed in between Now and 7:08:30PM.

In either case, you should be using date functions that were designed for
such calculations, like DateAdd or DateDiff.


Big Tony said:
I have a piece of code written in VS.NET 2003. It runs fine on one machine
and does not on another. It throws an invalid cast
 
Hi Big Tony,
I think it could be
\\\\
Dim SpecTime As Date = CDate("#07:08:30 PM#")
/////
To overcome this kind of problems: Set Option Strict in the top of your
program to On.
Than you will get a message when there is such a kind of an error.
When you think you don't need that, don't use it.

I hope this helps.

Cor
 
Back
Top