Formatting TimeSpan in C#

How can I format a Date Difference in C# using timespan.subtract

Here is what I have so far, I need it to return in hh:mi format

If there is an easier way to do it other than timespan im open to it.  I just have to return this to the user in an hh:mi format.  Even (negative times) -hh:mi if the time returned is negative.

Thanks
DateTime startTime = new DateTime(); 
 DateTime endTime = new DateTime();
 TimeSpan conflictDuration = new TimeSpan();

startTime = 03/28/2011 11:45;
endTime =   03/28/2011 1:47;

conflictDuration = endTime.Subtract(startTime);

Open in new window

jmpattonAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
p_davisConnect With a Mentor Commented:
string formatted = endTime.Subtract(startTime).ToString("hh:mm");
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Unfortunately the TimeSpan() class doesn't accept a format in ToString() so you have to manually extract the values and put them together yourself.  =\

Here's one way:
conflictDuration = endTime.Subtract(startTime);
string formatted = (conflictDuration.TotalMilliseconds < 0 ? "-" : "") + Math.Abs(conflictDuration.Hours).ToString("00") + ":" + Math.Abs(conflictDuration.Minutes).ToString("00");
MessageBox.Show(formatted);

Open in new window

0
 
p_davisCommented:
poo, i don't know what the heck i was thinking.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I've often wondered why they didn't implement a ToString() overload that accepts a format for the TimeSpan...it would make perfect sense to me!  =)
0
 
p_davisCommented:
it would be hard for a span that was more than a day, i guess?
0
 
wdosanjosCommented:
BTW, TimeSpan.ToString(format) is available in .NET 4:

    http://msdn.microsoft.com/en-us/library/dd992632.aspx

So, p_davis' answer is correct as long as it's .NET 4.

I hope this helps.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Woohoo!  Thanks for update wdosanjos.  =)

You have to escape the ":" though like this:
Dim dtA As DateTime = DateTime.Now
        Dim dtB As DateTime = dtA.AddDays(Math.PI)
        Dim ts As TimeSpan = dtB.Subtract(dtA)
        Label1.Text = ts.ToString("dd\:hh\:mm\:ss")

Open in new window


*This is different than the DateTime.ToString() method which is perfectly happy accepting an un-escaped colon!
0
 
p_davisCommented:
what is this dim you speak of??? ;p
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hahaha...grrrr....I'm always forgetting what ZONE I'm in!  =)

In not as beautiful as VB.Net, the C# equivalent would be:   *wink*
private void button1_Click(object sender, EventArgs e)
        {
            DateTime dtA = DateTime.Now;
            DateTime dtB = dtA.AddDays(Math.PI);
            TimeSpan ts = dtB.Subtract(dtA);
            label1.Text = ts.ToString(@"dd\:hh\:mm\:ss");
        }

Open in new window

0
 
p_davisCommented:
well defined space of a function embraced by beautiful curly brackets.....all is right with the world
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
// no comment;
0
 
p_davisCommented:

code puns =)

don't hate!
0
All Courses

From novice to tech pro — start learning today.