473,898 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 62764
I think you can do it like this:
TimeSpan ts = new TimeSpan(23,40, 3);
DateTime d = new DateTime(ts.Tic ks);
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
"RicercatoreSba dato" 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.TotalHo urs).ToString() .PadLeft(2, '0') +":" + s.Minutes.ToStr ing().PadLeft(2 , '0') + ":" + s.Seconds.ToStr ing().PadLeft(2 , '0');
On Sat, 05 Aug 2006 17:02:02 +0200, Muhammad Mosa <Mu**********@d iscussions.micr osoft.comwrote:
I think you can do it like this:
TimeSpan ts = new TimeSpan(23,40, 3);
DateTime d = new DateTime(ts.Tic ks);
string time = d.ToString("hh: mm:ss");


--
Happy coding!
Morten Wennevik [C# MVP]
Aug 5 '06 #3
Morten Wennevik <Mo************ @hotmail.comwro te:
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.TotalHo urs).ToString() .PadLeft(2, '0') +":" +
s.Minutes.ToStr ing().PadLeft(2 , '0') + ":" +
s.Seconds.ToStr ing().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.TotalHo urs,
ts.Minutes, ts.Seconds);

--
Jon Skeet - <sk***@pobox.co m>
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
11892
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
38708
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
5505
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 should be 0:zzz but I can't figure out the correct String.Format grammar to get the desired result. Can you? <%= Clinton Gallagher
13
2161
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 will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break start 10:30
33
4710
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
3
17184
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 is the fastest and the Regex.Matches.Count to be the slowest. I posted my results and source code to my web site. If you could take a look and maybe suggest an even faster method I'd like to hear from you. Also, if anyone can tell me why Regex is...
7
14985
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 first form, then from Hours into Seconds when a second form is loaded. Anyone got any ideas? If you need more info, let me know and ill post a code snippet or something.
1
2535
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 not in a correct format” Dim TimeLeft As Date = objPlayer.DateExpires.Subtract(TimeSpan.Parse(Now().ToLongTimeString)) Any idea why this is now broken? Paul.
5
4621
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 denominator for structs and strings and while looking through the SDK I only noticed the IEquatable<T> interface. So I implemented the class with methods such as the following, but I'm aware that this is not the right approach.
0
9842
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10859
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10487
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.