D
DWalker
In Visual Studio (Visual Basic) .NET 2002, I noticed that this:
Dim Elapsed as DateTime = Now - Now [or Now() - Now()]
gives a "compile time" error (error in the IDE), saying that the '-'
operator is not defined for the Date datatype. On the other hand,
Dim Elapsed as DateTime = Now + Now
does not give a compile time error but it thinks you're trying to
concatenate strings, and it fails at run-time.
I know about the Timespan class, which has a Subtract method:
Dim Elapsed as TimeSpan = Now.Subtract.StartTime [StartTime is a
datetime type]
Questions:
1) Why can't I add or subtract the current DateTime Now() value from
an earlier saved Now() value? I'm forced to use a Timespan object.
Why? Excel can add and subtract date/timestamps, VB can't?
2) When I have a timespan object, which of course represents an
elapsed time and can give me the number of hours, OR the number of
minutes, OR the number of seconds, etc., it's hard to get the elapsed
time formatted as hh:mm:ss. The ToString function returns
hh:mm:ss.fffffff and you have to search for and remove the fractional
part if you don't want it. If I wanted to see hours and minutes, would
I have to concatenate the hours part with a colon, then add the minutes
part?
The Date class accepts format strings on the ToString method, so this:
Something = Now.ToString("hh:mm:ss")
will work. But not with timespans. It's hard to get timespans
formatted for display in any arbitrary format that I might want, like
hours and minutes only.
In Excel and SQL, I have seen durations stored in datetime fields,
which is probably an abuse of the datatype, but it's convenient.
Am I missing anything or is it just this much of a pain to work with
elapsed times?
Thanks.
David Walker
Dim Elapsed as DateTime = Now - Now [or Now() - Now()]
gives a "compile time" error (error in the IDE), saying that the '-'
operator is not defined for the Date datatype. On the other hand,
Dim Elapsed as DateTime = Now + Now
does not give a compile time error but it thinks you're trying to
concatenate strings, and it fails at run-time.
I know about the Timespan class, which has a Subtract method:
Dim Elapsed as TimeSpan = Now.Subtract.StartTime [StartTime is a
datetime type]
Questions:
1) Why can't I add or subtract the current DateTime Now() value from
an earlier saved Now() value? I'm forced to use a Timespan object.
Why? Excel can add and subtract date/timestamps, VB can't?
2) When I have a timespan object, which of course represents an
elapsed time and can give me the number of hours, OR the number of
minutes, OR the number of seconds, etc., it's hard to get the elapsed
time formatted as hh:mm:ss. The ToString function returns
hh:mm:ss.fffffff and you have to search for and remove the fractional
part if you don't want it. If I wanted to see hours and minutes, would
I have to concatenate the hours part with a colon, then add the minutes
part?
The Date class accepts format strings on the ToString method, so this:
Something = Now.ToString("hh:mm:ss")
will work. But not with timespans. It's hard to get timespans
formatted for display in any arbitrary format that I might want, like
hours and minutes only.
In Excel and SQL, I have seen durations stored in datetime fields,
which is probably an abuse of the datatype, but it's convenient.
Am I missing anything or is it just this much of a pain to work with
elapsed times?
Thanks.
David Walker