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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With