473,385 Members | 1,782 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.

string.format and timespan

I'd like to print a TimeSpan variable in this way:

00:00:00 --hh:mm:ss

(whitout the milliseconds)

how can I do it?

Aug 5 '06 #1
3 62669
I think you can do it like this:
TimeSpan ts = new TimeSpan(23,40,3);
DateTime d = new DateTime(ts.Ticks);
string time = d.ToString("hh:mm:ss");
--
Muhammad Mosa
Software Engineer & Solution Developer
MCT/MCSD.NET
MCTS: .Net 2.0 Web Applications
MCTS: .Net 2.0 Windows Applications
"RicercatoreSbadato" wrote:
I'd like to print a TimeSpan variable in this way:

00:00:00 --hh:mm:ss

(whitout the milliseconds)

how can I do it?

Aug 5 '06 #2
00:00:00 can't be displayed as hh:mm:ss though. You need HH:mm:ss for that.

If you need to show total hours this method won't work if the TimeSpan exceeds 24 hours.
If this is an issue you can always format your string the hard way

TimeSpan s = dateTimeB - dateTimeA;
string time = ((int)s.TotalHours).ToString().PadLeft(2, '0') +":" + s.Minutes.ToString().PadLeft(2, '0') + ":" + s.Seconds.ToString().PadLeft(2, '0');
On Sat, 05 Aug 2006 17:02:02 +0200, Muhammad Mosa <Mu**********@discussions.microsoft.comwrote:
I think you can do it like this:
TimeSpan ts = new TimeSpan(23,40,3);
DateTime d = new DateTime(ts.Ticks);
string time = d.ToString("hh:mm:ss");


--
Happy coding!
Morten Wennevik [C# MVP]
Aug 5 '06 #3
Morten Wennevik <Mo************@hotmail.comwrote:
00:00:00 can't be displayed as hh:mm:ss though. You need HH:mm:ss for that.

If you need to show total hours this method won't work if the TimeSpan exceeds 24 hours.
If this is an issue you can always format your string the hard way

TimeSpan s = dateTimeB - dateTimeA;
string time = ((int)s.TotalHours).ToString().PadLeft(2, '0') +":" +
s.Minutes.ToString().PadLeft(2, '0') + ":" +
s.Seconds.ToString().PadLeft(2, '0');
Or, slightly more concisely:

TimeSpan ts = new TimeSpan(12, 6, 7);
string x = string.Format ("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours,
ts.Minutes, ts.Seconds);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug | last post by:
Hi, I was wondering if someone could tell me how i can convert a value that's a string into a TimeSpan? Thanks, Doug
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.
4
by: clintonG | last post by:
TimeSpan utcOffset = localZone.GetUtcOffset(DateTime.Now); MessageLabel.Text = utcOffset.ToString( ); // returns -06:00:00 Instead of -06:00:00 I want -06:00. Apparently the custom formatter...
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...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
3
by: Dana King | last post by:
I'm looking for some other developers opinions, I'm trying to find the best way to count strings within a string using VB.net. I have tested five methods and have found the String.Replace method...
7
by: WorldIsEnding | last post by:
Hey, Im having trouble with converting a Numeric value (such as a single digit number like 1 or 5) into a time format. I need to convert a number entered into a variable into Hours on the...
1
by: =?Utf-8?B?UGF1bCBIYWxl?= | last post by:
I recently moved an asp.net project from win server 2003 \ IIS 6 \ Framework 2.0 to Win server 2008 \ IIS 7 \ Framework 3.5. Now the following line of code returns an error “Input string was...
5
by: Anders Borum | last post by:
Hi! While implementing a property manager (that supports key / value pairs), I was wondering how to constrain T to a struct or string type. Basically, I guess what I'm looking for is the common...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.