Round time to 5min interval in Lua
snippet in lua

Round time to 5min interval in Lua

user6211

local function round5min(var)
   local h, m, ampm = var:match"^(%d+):(%d+)(%a+)$"
   local t = (({am=0,pm=12})[ampm:lower()]+h%12)*60+m+2
   t = t-t%5
   m = t%60
   t = (t-m)/60
   h = t%12
   return ("%d:%02d%s"):format((h-1)%12+1, m, ({"am","pm"})[(t-h)/12%2+1])
end

print(round5min("9:23am"))   --> 9:25am
print(round5min("11:56am"))  --> 11:55am
print(round5min("11:57am"))  --> 11:55am
print(round5min("11:58am"))  --> 12:00pm
print(round5min("11:59am"))  --> 12:00pm
print(round5min("11:59pm"))  --> 12:00am