473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Timespan and time formatting

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
2 19026
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pete | last post by:
I need to take many TimeSpan values and find their average. I know you can Add the many values together, but how do you then divide that result by the count of values that you had? Once I have...
2
by: Jeff Shantz | last post by:
Hello, I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an...
3
by: Ivan A. | last post by:
Hi! Why I can't serialize TimeSpan structure with XmlSerializer? This is what I do: using System; using System.IO; using System.Xml; using System.Xml.Serialization;
1
by: Tales Normando | last post by:
Hi, How can I format a timespan to show only the hours, minutes and seconds? I don't really like how it adds the days information on the returned ToString() string. Tales
10
by: Charles Law | last post by:
If I display a TimeSpan I get something like 00:05:17.6217891 when what I would like to see is 00:05:18 Is there an easy way to get this output? Try as I might I just can't find it.
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
3
by: Horselover Fat | last post by:
Hi, I have a column in a database that holds elapsed time in milliseconds and I want to display that column in a DataGrid in a specific format (d.hh:mm:ss.fff). However, the closest I can get...
2
by: Bob | last post by:
Greetings All, Is there a simple way of suppressing the fractional part of a timespan At the moment I use one time span to populate another minus the fractional part .. this is a bit clunky. ...
2
by: =?Utf-8?B?WVhR?= | last post by:
Hello, I want to convert the timespan (1.12:25:23) to 1day 12hour 25min 23sec, how to do it? thank you.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.