Timespan and time formatting

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
 
J

Jay B. Harlow [MVP - Outlook]

DWalker,
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.
VB.NET 2002 & 2003 do not support operator overloading, you can instead use:

Dim elapsed As TimeSpan = Now.Subtract(Now)

(DateTime also has a SubTract method).

VB.NET 2005 (aka Whidbey, due out later in 2005) will support operator
overloading, so you can then use:

' VB.NET 2005 syntax
Dim elapsed As TimeSpan = Now - Now
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
Unfortunately TimeSpan does not have an overloaded ToString that allows
custom formating. Its been discussed in the beta newsgroups for VB.NET 2005,
however I'm not sure if VB.NET 2005 will receive it or not.

As a workaround I convert the TimeSpan to a DateTime & format the DateTime.
Something = DateTime.MinValue.Add(elapsed).ToString("hh:mm:ss")

You just need to make sure elapsed is positive!

Hope this helps
Jay



DWalker said:
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
 
D

DWalker

The MinValue and MaxValue fields in TimeSpan and DateTime (and Zero in
TimeSpan) were confusing. Your example has clarified what they are good
for. So:

Something = DateTime.MinValue.Add(elapsed).ToString("hh:mm:ss")

adds the timespan in Elapsed to the smallest datetime, thereby converting
the timespan to a datetime (then formats it). Interesting. No direct
casts....

And I hadn't thought about using Subtract from DateTime. Of course, I'm
not really subtracting Now from Now, but storing Now and then doing some
stuff and then looking at Now.


Thanks, I appreciate the help.

David Walker


DWalker,
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.
VB.NET 2002 & 2003 do not support operator overloading, you can
instead use:

Dim elapsed As TimeSpan = Now.Subtract(Now)

(DateTime also has a SubTract method).

VB.NET 2005 (aka Whidbey, due out later in 2005) will support operator
overloading, so you can then use:

' VB.NET 2005 syntax
Dim elapsed As TimeSpan = Now - Now
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
Unfortunately TimeSpan does not have an overloaded ToString that
allows custom formating. Its been discussed in the beta newsgroups for
VB.NET 2005, however I'm not sure if VB.NET 2005 will receive it or
not.

As a workaround I convert the TimeSpan to a DateTime & format the
DateTime.
Something = DateTime.MinValue.Add(elapsed).ToString("hh:mm:ss")

You just need to make sure elapsed is positive!

Hope this helps
Jay



DWalker said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top