Rounding %Hours %Minutes Time?

On exporting files I like to have a folder with date/time but I’m trying to find a way to round the time down to the last half hour or quarter hour. So Instead of something like ‘1217’ it would be 1200 or ‘0937’ would be ‘0930’

Is this possible without a bunch of math for each minute possible?

I’ve done something very similar. Here’s a little function to do custom rounding:

def myround(x, base=5):
	"""little helper function to round x to base"""
	return int(base * round(float(x)/base))

Use it like this:

now = datetime.datetime.now()

# round to 5 minutes
mins = myround(now.strftime('%M'))

# round to 15 minutes
mins = myround(now.strftime('%M'), 15)

1 Like

Ah - very nice!

Another solution I found that works specifically with datetime. Change the 15’s to whatever increment you want:

        date = datetime.datetime.now()
        approx = round(date.minute/15.0) * 15

        date = date.replace(minute=0)
        date += datetime.timedelta(seconds=approx * 60)
        time = date.time()
2 Likes