Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting subtracted dates c#

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!

like image 905
HummingbirdHolly Avatar asked Apr 19 '26 17:04

HummingbirdHolly


1 Answers

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

like image 145
Tim Schmelter Avatar answered Apr 22 '26 08:04

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!