3

I am trying to subtract two dates from each other in c#. My current code looks like this:

protected void Page_Load(object sender, EventArgs e)
{
    System.DateTime matchStart = new System.DateTime(2012, 10, 17, 20, 00, 00);
    System.DateTime currentDateTime = new System.DateTime(2012, 10, 9, 14, 00, 00);

    System.TimeSpan matchCountdown = matchStart.Subtract(currentDateTime);

    countdown.Text = matchCountdown.ToString();
}

This currently gives me the result "8.06:00:00". What I am trying to do however, is to get the time difference formatted so it says "8 days, 6 hours, 0 minutes". How on earth do I get on about doing this?

Any help is much appreciated!

1 Answers1

5

You can use String.Format and the TimeSpan properties.

String.Format("{0} days, {1} hours, {2} minutes"
    , matchCountdown.Days
    , matchCountdown.Hours
    , matchCountdown.Minutes); 

Here's the demo: http://ideone.com/i8SKX

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939