[.NET]取得datetime變數之間的差異秒數

  • 170
  • 0

[.NET]取得datetime變數之間的差異秒數

DateTime datevalue1 = new DateTime();
DateTime datevalue2 = new DateTime();
datevalue1 = Convert.ToDateTime( "2019-02-21 15:00:00");
datevalue2 = Convert.ToDateTime( "2019-02-21 16:00:00");
var seconds = (datevalue1 - datevalue2).TotalSeconds;
Console.WriteLine(seconds);


執行結果:



參考資料:
Showing Difference between two datetime values in hours
https://stackoverflow.com/questions/4946316/showing-difference-between-two-datetime-values-in-hours