现在和下一小时之间的时间跨度?

发布于 2024-11-02 16:25:29 字数 128 浏览 4 评论 0原文

现在是 8:30,我想找出从现在到下一个整小时 (9:00) 之间有多少秒。我想我只想 DateTime.Now.AddHours(1) 但在我这样做之后,我认为我需要“地板”。如何获得该值?

谢谢。

It is 8:30 and I am trying to find out how many seconds there are between now and the next whole hour (9:00). I think I just want to DateTime.Now.AddHours(1) but after I do that I think I need the "floor". How to get that value?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

谁人与我共长歌 2024-11-09 16:25:29

只需将一天中的时间(以小时为单位)向上舍入到下一个整数值:

var timeOfDay = DateTime.Now.TimeOfDay;
var nextFullHour = TimeSpan.FromHours(Math.Ceiling(timeOfDay.TotalHours));
var delta = (nextFullHour - timeOfDay).TotalSeconds;

Just round the time of day in hours up to the next integral value:

var timeOfDay = DateTime.Now.TimeOfDay;
var nextFullHour = TimeSpan.FromHours(Math.Ceiling(timeOfDay.TotalHours));
var delta = (nextFullHour - timeOfDay).TotalSeconds;
烙印 2024-11-09 16:25:29

您不必乱搞天花板和地板。 DateTime.Hour 属性表示 DateTime 所表示的一天中的小时(它是 0 到 23 之间的整数)。您可以使用它和 DateTime。 Date 属性来去除您不需要的 DateTime 的组成部分(不到一小时的数据),然后根据需要进行减去以生成 TimeSpan

var now = DateTime.Now;
var timeToNextHour = now.Date.AddHours(now.Hour + 1) - now;

如果您想要以秒为单位的结果,您当然可以提取结果 TimeSpanTotalSeconds 部分。

You don't have to mess around with ceilings and floors. The DateTime.Hour property represents whole hours (it is an integer beteen 0 and 23) of the time of the day represented by the DateTime. You can use this and the DateTime.Date property to strip the components of the DateTime you don't want (sub-hour data) and then just subtract as necessary to produce a TimeSpan.

var now = DateTime.Now;
var timeToNextHour = now.Date.AddHours(now.Hour + 1) - now;

You can of course extract the TotalSeconds component of the resulting TimeSpan if you want the result in seconds.

另类 2024-11-09 16:25:29

//完全误读。完全重写

我只会做这样的事情

int minutesToNextHour = 60 - DateTime.Now.Minutes;
int secondsToNextHour = minutesToNextHour * 60;

//Completely misread. Completely re-writing

I woudl just do something Like this

int minutesToNextHour = 60 - DateTime.Now.Minutes;
int secondsToNextHour = minutesToNextHour * 60;
极度宠爱 2024-11-09 16:25:29

这似乎是最简单的:(

3600 - DateTime.Now.TimeOfDay.TotalSeconds % 3600

如果您想要整数 - 整数 - 然后在 DateTime.Now... 前面加上 (int)

This seems to be the most simple:

3600 - DateTime.Now.TimeOfDay.TotalSeconds % 3600

(if you want it in whole numbers - integer - then prefix DateTime.Now... with (int).

他不在意 2024-11-09 16:25:29

所以你需要减去“剩余”分钟,找到差值,然后乘以 60,对吧?

So you'd need to subtract the 'remainder' minutes, find the difference, and multiply that by 60, right?

清晰传感 2024-11-09 16:25:29

这个怎么样:

 var currentTime = DateTime.Now;
 var hour = currentTime.AddHours(1).Hour;
 var newTime = Convert.ToDateTime(hour + ":00");
 var timespan = newTime.Subtract(currentTime);
 var secondsDiff = timespan.TotalSeconds;

How about this:

 var currentTime = DateTime.Now;
 var hour = currentTime.AddHours(1).Hour;
 var newTime = Convert.ToDateTime(hour + ":00");
 var timespan = newTime.Subtract(currentTime);
 var secondsDiff = timespan.TotalSeconds;
行至春深 2024-11-09 16:25:29
TimeSpan sec = new TimeSpan(0, 0, 3600 - (DateTime.Now.Minute * 60));
TimeSpan sec = new TimeSpan(0, 0, 3600 - (DateTime.Now.Minute * 60));
最丧也最甜 2024-11-09 16:25:29

怎么样:

var now = DateTime.Now;
int secondsTillNextHour = (60 - now.Minute)*60+(60-now.Second);

或者(也许更清楚):

int SecondsTillNextHour = 3600 - 60*now.Minute - now.Second;

How about:

var now = DateTime.Now;
int secondsTillNextHour = (60 - now.Minute)*60+(60-now.Second);

Or (maybe clearer):

int SecondsTillNextHour = 3600 - 60*now.Minute - now.Second;
漫漫岁月 2024-11-09 16:25:29

更易读的版本:

public double SecondsToNextHour()
{
  return SecondsToNextHour( DateTime.Now );
}

public double SecondsToNextHour( DateTime moment )
{
  DateTime currentHour = new DateTime( moment.Year, moment.Month, moment.Day, moment.Hour, 0, 0 );
  DateTime nextHour = currentHour.AddHours( 1 );
  TimeSpan duration = nextHour - moment;
  return duration.TotalSeconds;
}

A more readable version:

public double SecondsToNextHour()
{
  return SecondsToNextHour( DateTime.Now );
}

public double SecondsToNextHour( DateTime moment )
{
  DateTime currentHour = new DateTime( moment.Year, moment.Month, moment.Day, moment.Hour, 0, 0 );
  DateTime nextHour = currentHour.AddHours( 1 );
  TimeSpan duration = nextHour - moment;
  return duration.TotalSeconds;
}
吐个泡泡 2024-11-09 16:25:29
TimeSpan result =  (new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0)).Subtract(DateTime.Now);

基本上,您正在构建一个新的日期时间,该日期时间是从现在开始一小时,没有分钟或秒,然后从中减去现在并得到结果。

TimeSpan result =  (new DateTime(DateTime.Now.Year, DateTime.Now.Month, 
DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0)).Subtract(DateTime.Now);

Basically here you are building a new DateTime that is one hour on from Now, with no minutes or seconds, then you subtract Now from this and have your result.

弥枳 2024-11-09 16:25:29

我会 Timespan.Parse 08:30,向对象添加 1 小时,然后检索小时部分并使用 :00 作为分钟构建一个新字符串,并重新解析新字符串。可能有一种更有效的方法来做到这一点,但我发现这种技术易于阅读。

I would Timespan.Parse 08:30, add 1 hr to the object, then retrieve the hour part and build a new string with :00 as the minutes and reparse the new string. There may be a more efficient way to do this, but I find this technique clear to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文