Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
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);


Thanks in advance. I would really appreciate answers!

Thank you!
Posted
Updated 9-Mar-21 19:49pm
v2
Comments
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...

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


Assuming d.Ticks is 15 minutes.
 
Share this answer
 
 
Share this answer
 
Comments
bjay tiamsic 5-Jan-16 1:43am    
Thank you so much! This is the best solution!
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,
                        dateTime.Day, dateTime.Hour, 0, 0).AddHours(1);
                    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;
       }
 
Share this answer
 
v5
Comments
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900