Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut-/Rounding down a DateTime to a Whole i.e. Hour/Day?

Tags:

c#

Is there any kind of mathematical way to cut DateTime down to a exact Hour, Day or so? Similiar to round of a decimal to int.

Period.Day
If the original value was 2011-01-01 13:00:00, it ends up in 2011-01-01 00:00:00

if Period.Hour
If the original value was 2011-03-11 13:32:00, it ends up in 2011-03-11 13:00:00

I think about something like below. This are of course works fine, but the range-array are iterated through anyway, later. Better if I was possible to calculate directly on that iteration, instead of it's own. But someType can't be put into that iteration (it depends on someType).

if (someType == Period.Day)
  range.ForEach(d => d.time = new DateTime(d.time.Year, d.time.Month, d.time.Day,0,0,0));
if (someType == Period.Hour)
  range.ForEach(d => d.time = new DateTime(d.time.Year, d.time.Month, d.time.Day, d.time.Hour, 0, 0));
like image 988
Independent Avatar asked Nov 10 '11 14:11

Independent


People also ask

How do you round down a time in Python?

The round() function rounds a number to the nearest whole number. The math. ceil() method rounds a number up to the nearest whole number while the math. floor() method rounds a number down to the nearest whole number.

How do you round the minutes in a DateTime object?

In order to round a DateTime object to the nearest minute, you need to use the round operation from Pandas on the DateTime column and specify the frequency that you want to use. For rounding to the nearest minute you will need to use round("min") .

How do I remove seconds from DateTime?

Solution 1 Using different methods on the DateTime, like . Today give the date part, but the other parts of it are zero. This is by design. If you don't want to display the seconds when you create the string, use a format string, like MM/dd/yyyy hh.mm and leave the tt part off.

Is there a date data type in C#?

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D.


3 Answers

Rounding down to a day is equivalent to time.Date, rounding to nearest (up on midpoint) is simply ( time + 12hours ).Date.

For rounding down to a full hour I can't think of code that's nicer to read than yours. For rounding up to the nearest hour you can apply your code to time + 30mins.

There is probably a faster method for rounding to the nearest hour:

const Int64 HourInTicks=...;
Int64 timeInTicks=time.Ticks;
Int64 trucatedToHour=timeInTicks-timeInTicks%HourInTicks;

But I'd avoid that, unless you really need the performance, which is unlikely.

(My round to nearest might have issues on days where the local time offset changes if you're using local time)

like image 138
CodesInChaos Avatar answered Oct 18 '22 02:10

CodesInChaos


To round down to day you can use the DateTime.Date Property.
To round down to hour, I'm afraid you'll have to either use what you did in your example or something like:

d.Date.AddHours(d.Hour)
like image 28
Svarog Avatar answered Oct 18 '22 04:10

Svarog


I'll do the following:

private static readonly DateTime Epoch = new DateTime(1970, 1, 1); 
public static DateTime Round(this DateTime d, Period p)
{
    var ts = d - Epoch;

    if (p == Period.Hour)
    {
        var hours = (long)ts.TotalHours;
        return Epoch.AddHours(hours);
    }
    else if (p == Period.Days)
    {
        var days = (long)ts.TotalDays;
        return Epoch.AddDays(days);
    }
    // ...
}
like image 42
Arnaud F. Avatar answered Oct 18 '22 04:10

Arnaud F.