15,181,010 members
See more:
Hi. Does anyone here know how to round down a time to nearest 15-minute interval

For example:

7:47AM should be 7:45AM
8:15AM remains 8:15AM
10:59AM should be 10:45AM

I have found this question but it ROUNDS UP to the nearest 15minutes.
C#
`DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks);`

Thank you!
Posted
Updated 9-Mar-21 20:49pm
v2
Patrice T 4-Jan-16 22:05pm

What have you done so far ?
bjay tiamsic 4-Jan-16 22:22pm

Here. But it rounds up

DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks);
Philippe Mori 5-Jan-16 19:49pm

If you already have that, then it is trivial...

## Solution 2

You should try:
C#
`DateTime((dt.Ticks / d.Ticks) * d.Ticks);`

Assuming `d.Ticks` is 15 minutes.

## Solution 1

bjay tiamsic 5-Jan-16 1:43am

Thank you so much! This is the best solution!

## Solution 4

Finding an issue with solution
`DateTime(((dt.Ticks + d.Ticks - 1) / d.Ticks) * d.Ticks);`

Date `2/2/2021 9:11:30 PM` rounding down to `2/2/2021 9:30:00 PM`.
But I am expecting `2/2/2021 9:00:00 PM` here.

You can try any of the below solutions, both are the same, but for different versions of C#. Please note, these are for 30 min roundup (that is why divided by 15), if need to be 15 mins, you need to add some modifications.

#1.
```DateTime RoundUp(DateTime dateTime)
{
var minute = dateTime.Minute;

var retDateTime = dateTime;

switch (minute / 15)
{
case 0:
retDateTime = new DateTime(dateTime.Year, dateTime.Month,
dateTime.Day, dateTime.Hour, 0, 0);
break;
case 1:
retDateTime = new DateTime(dateTime.Year, dateTime.Month,
dateTime.Day, dateTime.Hour, 30, 0);
break;
case 2:
retDateTime = new DateTime(dateTime.Year, dateTime.Month,
dateTime.Day, dateTime.Hour, 30, 0);
break;
case 3:
retDateTime = new DateTime(dateTime.Year, dateTime.Month,
break;
}

return retDateTime;
}```

#2.
```DateTime RoundUp(DateTime dateTime)
{
var minute = dateTime.Minute;

var retDateTime = (minute / 15) switch
{
0 => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0),
1 => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 30, 0),
2 => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 30, 0),
3 => new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0).AddHours(1),
_ => dateTime
};

return retDateTime;
}
```
v5
Richard MacCutchan 10-Mar-21 4:01am

Did you notice that a solution was provided and accepted five years ago?
sharoncek@gmail.com 10-Mar-21 21:54pm

Yes Richard, my intention is, if someone still looking for a solution, this may be helpful for them.