Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timespan between Now and Next Hour?

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.

like image 592
Snowy Avatar asked Apr 20 '11 16:04

Snowy


People also ask

What is the difference between DateTime and TimeSpan?

The TimeSpan struct represents a duration of time, whereas DateTime represents a single point in time. Instances of TimeSpan can be expressed in seconds, minutes, hours, or days, and can be either negative or positive.

How do I get the time difference between two times in C#?

DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25); Now, get the difference between two dates. TimeSpan ts = date2 - date1; Get the result i.e. the difference in hours.

What is TimeSpan C#?

TimeSpan(Int32, Int32, Int32, Int32) Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, and seconds. TimeSpan(Int32, Int32, Int32, Int32, Int32) Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds, and milliseconds.

How do I convert DateTime to TimeSpan?

To convert a DateTime to a TimeSpan you should choose a base date/time - e.g. midnight of January 1st, 2000, and subtract it from your DateTime value (and add it when you want to convert back to DateTime ). If you simply want to convert a DateTime to a number you can use the Ticks property. Save this answer.


1 Answers

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).

like image 112
Lasse Espeholt Avatar answered Sep 28 '22 05:09

Lasse Espeholt