By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

Timespan and time formatting

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <no**@none.com> wrote in message
news:e9****************@TK2MSFTNGP15.phx.gbl... 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

Nov 21 '05 #2

P: n/a
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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in
news:uC*************@TK2MSFTNGP12.phx.gbl:
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" <no**@none.com> wrote in message
news:e9****************@TK2MSFTNGP15.phx.gbl...
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



Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.