How can I round time (format HH:mm) to quarters?
For example:
time = 12:26:44 > 12:30
or
time = 12:35:21 > 12:30
How can I round time (format HH:mm) to quarters?
For example:
time = 12:26:44 > 12:30
or
time = 12:35:21 > 12:30
my guess is that you can use the modulus function on the number of minutes, pseudocode:
mins = (mins % 15) * 15;
swingman:
mins = (mins % 15) * 15;
Swingman, thanks for thinking along, but that didn’t do it unfortunatly… it just multiplied the minutes times 15… any other suggestions?
freecolours:
swingman:
mins = (mins % 15) * 15;Swingman, thanks for thinking along, but that didn’t do it unfortunatly… it just multiplied the minutes times 15… any other suggestions?
That’s why he said it is pseudocode. If you use it literally it obviously doesn’t work. Having said that I think it is better to use the round function to round to 15 minutes, here’s some real (non pseudo) code:
//this code assumes you put your datetime in the var named a
a.setMinutes(Math.round(a.getMinutes()/15)*15)
a.setSeconds(0)
a.setMilliseconds(0)
there is still a flaw in the above; if the time eg is 12:22:31 it will round it to 12:15. You can fix that by adding a few lines (some minor thinking will be required but not much). I’m also sure that the true javascript wizards (like Enrico) can actually do the whole rounding in one line of Javascript
You can add 7.5 minutes to the minutes before applying the formula…
Here is some more pseducode
From 0 up to 7.5 mins becomes 0
From 7.5 up to 22.5 mins become 15
etc…
Warning: the last 7.5 mins of the hour also becomes 0, and you’ll have to add an hour.
Let’s see if one of the Javascript wizards can make a one-liner out of this!
swingman:
You can add 7.5 minutes to the minutes before applying the formula…
That wouldn’t get the required result. Adding 29 seconds to it would be sufficient.
Anyway…the following code should do it.
var dt = new Date();
dt = new Date((Math.round(dt/1000/60/15))*15*60*1000);
It’s a one-liner alright.
Maybe you can try with:
var min_full = initial_date.valueOf(); // milliseconds of initial date
var min_zero = new Date(initial_date.getFullYear(), initial_date.getMonth(), initial_date.getDate()).valueOf(); // same date in milliseconds with no time
var delta = min_full - min_zero + 4.5E5; // +4.5E5 if you want to round to de nearest quarter
rounded_date = new Date(min_zero + delta - (delta%9e5)); // 9e5 is the number of millisecond in a quarter of hour
Thanks for the appreciation brasaap… maybe can be done in just one line.. but I think the 4 lines solution is more readeable…
BTW
The modulo function is the integer remainder of dividing var1 by var2. For example, 14 % 5 returns 4.
Hi Robert, I was already thinking for a tip for Servoy Magazine … but now your method is a lot smarter
than mine… BRAVO!! so is your turn to write to Servoy Magazine
Let me explain my one-liner:
A Date has in fact a value in milliseconds counting from Jan 01 01:00:00 CET 1970.
So adding or substracting milliseconds to it takes care of any hour or day changes when calculating with time.
As brasaap already explained you can devide the minutes by 15, round that and then multiply that with 15 again to get minutes again.
So what the one-liner does is this.
date_in_milliseconds divided by 1000 to make seconds,
divided by 60 to make minutes,
divided by 15 to make the quarters of an hour.
Round that to whole numbers.
And now back to milliseconds again:
result_of_rounding multiplied by 15 to make minutes,
multiplied by 60 to make seconds,
multiplied by 1000 to make milliseconds.
Now put that large number inside a Date() object and you get a nice rounded datetime.
The JavaScript award of the day goes to Robert!
automazione:
Hi Robert, I was already thinking for a tip for Servoy Magazine … but now your method is 8) a lot smarter 8) than mine… BRAVO!! so is your turn to write to Servoy Magazine
Thanks! Good idea.
As you can see I already explained the one-liner. I will post it on Servoy Magazine.
ROCLASI:
var dt = new Date();
dt = new Date((Math.round(dt/1000/60/15))1560*1000);
It's a one-liner alright. <img src="{SMILIES_PATH}/icon_wink.gif" alt=";)" title="Wink" />
Awesome method!! Great mathwork Robert, thanks a lot! Got some nice features added to my app with this method.
Thanks everybody in this post for thinking alongside, too!
Next issue came along this topic;
I use this calc for counting workinghours:
var newie = new Date(1970,0,1,0,0,0,0);
if(from && untill)
{
var dif = (untill - from) / (1000 * 60 * 60);
}
else
{
var dif = 0 ;
}
if(pauze)
{
var breaking = (pauze - newie) / (1000 * 60 * 60) ;
}
else
{
var breaking = 0 ;
}
var number = dif - breaking;
return number
Now when I use the method from Robert to set the ‘from’ time, and I select an ‘untill’ time (from a valuelist), then the calculation returns a ‘-311655,00’ number.
For example: from time is set to ‘14:00:00’ and I choose ‘22:00:00’ as untill time (from the valuelist), then the calc returns ‘-311655,00’.
I guess something is wrong in my calc, but I don’t know what…
Can you see what values From and Untill exactly have (btw until is with 1 L )
Also what value does Pauze have?
If it’s a datetime then check what dates they use.