开发者

.Net DateTime.Subtract Issue

开发者 https://www.devze.com 2023-01-08 04:47 出处:网络
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 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.


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");


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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号