我知道如何计算两个日期之间的差异,但我如何计算给定日期和下一个上午8点之间的时间?
答案 0 :(得分:14)
var now = DateTime.Now;
var tomorrow8am = now.AddDays(1).Date.AddHours(8);
double totalHours = ( tomorrow8am - now).TotalHours;
答案 1 :(得分:2)
var now = DateTime.Now;
double diffHours = 24 - (now - now.Date).TotalHours + 8;
答案 2 :(得分:0)
这样的事情
var now = DateTime.Now();
var target = DateTime.Today.AddDays(1).AddHours(8);
var result = (target - now).Hour;
答案 3 :(得分:-1)
在同一天早上8点之前运作良好。
DateTime now = DateTime.Now;
DateTime next8am =now.Date.AddHours(8);
if(now.TimeOfDay>TimeSpan.FromHours(8))
next8am.AddDays(1);
return next8am.Subtract(now).TotalHours