tags:

views:

73

answers:

4

I have a function that returns the difference between 2 DateTime's in seconds. However, in certain cases its not working correctly and I'm not sure why.

I.E.:

Debug.WriteLine(DateTime.Parse("7/22/2010 9:52:39 AM").Subtract(DateTime.Parse("7/22/2010 8:58:38 AM")).Seconds, "WTF");

The above code returns 1 ... obviously there's more than a 1 second difference between the dates above.

+14  A: 

Use the TimeSpan.TotalSeconds property instead of .Seconds

Debug.WriteLine(DateTime.Parse("7/22/2010 9:52:39 AM").Subtract(DateTime.Parse("7/22/2010 8:58:38 AM")).TotalSeconds, "WTF");
Justin
+1  A: 

I think you should use the .TotalSeconds property. The difference in the two dates is just one second + plus a couple of minutes and so on.

JWL_
A: 

You're printing just the seconds portion. Try this

Debug.WriteLine(DateTime.Parse("7/22/2010 9:52:39 AM").Subtract(DateTime.Parse("7/22/2010 8:58:38 AM")), "WTF"); 

// Prints 00:54:01
Winston Smith
+3  A: 

You'll need to get TotalSeconds, not just seconds; the difference is 54 min, 1 second;

riffnl