Using moment js to round a given number of minutes - javascript

If I have the number of minutes, say 476, which equals 7 hours and 56 minutes and I need to round this number up to the nearest half hour, so in this instance I need to round 476 so that it equals to 8 hours. How could I do this in moment js?

You can use following logic:
Logic:
Get the total number of hours by dividing minutes by 60.
Get remainder minutes using mins % 60.
If mins is between 15 and 45, we can round it to 30.
If it is less than 15 or more than 45, we can round it to 0`.
If mins is greater than or equal to 45, we will increment hour by 1.
function getTime(min){
var hours = Math.floor(min/60);
var mins = min % 60;
console.log('remainder mins: ', mins)
if(mins >= 15 && mins < 45)
mins = 30;
else{
hours += mins > 45 ? 1 : 0;
mins = 0;
}
return "" + hours + (mins ? ":" + mins : "");
}
console.log(getTime(476))
console.log(getTime(446))
console.log(getTime(426))
console.log(getTime(576))

Related

Converting minutes to days, hours, and remaining minutes

Trying to convert minutes per total number of episodes into days, hours, and minutes but I can't figure out the proper formula for hours.
function minToDays(min, ep) {
let days = parseInt((min * ep) / 1440);
let hours = parseInt(((min * ep) % 60));
let remainingMin = hours % 60;
return `${days} day(s) and ${hours} hour(s) and ${remainingMin} minutes(s).`;
}
console.log(minToDays(20, 800)); //11 days, 2 hours, 40 minutes
A bit clunky, but here's a quick way to see the logic - each time you need to see what minutes remain. I'm sure someone with more time will give you a more elegnat answer:
function minToDays(min, ep) {
let days = Math.floor((min * ep) / 1440);
let remainingTime = parseInt((min*ep) - Math.floor((days *1440)));
let hours = Math.floor((remainingTime / 60));
let remainingMin = Math.floor(remainingTime - (hours*60));
return `${days} day(s) and ${hours} hour(s) and ${remainingMin} minutes(s).`;
}
console.log(minToDays(20, 800)); //11 days, 2 hours, 40 minutes
This is textbook problem. The operator is called Remainder (%)
What is Remainder: This originated from Math, or I think this is plain Math operator, used in Programming and in College Math.
How Remainder Operator is used/defined
Remainder operator (I know this operator as Modulo operator from Mathematics) just calculates the Remainder.
101%10-> 1
81%7 ->4
81/7 -> 11.57
So, Remainder Operator only calculates the remaining after division.
this should do.
function minutesToDays(min,ep ) {
let totalMinutes = min * ep,
cd = 24 * 60 ,
ch = 60 ,
day = Math.floor(totalMinutes / cd),
hour = Math.floor((totalMinutes - day * cd) / ch),
minute = Math.round(totalMinutes - day * cd - hour * ch),
pad = n => { return n < 10 ? '0' + n : n; };
if (minute === 60) {
hour++;
minute = 0;
}
if (hour === 24) {
day++;
hour = 0;
}
return [(day > 0 ? `${day} days` : ""), (hour > 0 ? `${pad(hour)} hours ` : ''), `${pad(minute)} minutes`].join(" ");
}
console.log(minutesToDays(20, 800))

Convert second to years, days, hours, minutes and seconds

help me with this one problem. I am confused how to convert second to years, days, hours, minutes and second in JavaScript. This below as an example
convertSeconds(10000)) // 2 hours, 46 minutes and 40 seconds
convertSeconds(62)) // 1 minutes and 2 seconds
convertSeconds(2000000)) // 23 days, 3 hours, 33 minutes and 20 seconds
convertSeconds(126144060)) // 4 years and 1 minutes
I know this task needs modulus just like below :
var days = Math.floor(seconds / (3600*24))
seconds -= days*3600*24
var hrs = Math.floor(seconds / 3600)
seconds -= hrs*3600
var minutes = Math.floor(seconds / 60)
seconds -= minutes*60
But it doesn't print as I want it like in my comments. How to console like that. Thank you
if I understand your question, the solution could be:
function convertSeconds(secT){
var seconds = secT % 60;
var minutes = ((secT - seconds)/60) % 60;
var hours = (secT - seconds - (minutes * 60)) / 3600 % 3600;
//EDIT
var print = "";
If(hours!=0){
print = print + hours + " hours ";
}
if(minutes!=0){
print = print + minutes + " minutes ";
}
if(seconds!=0){
print = print + seconds + " seconds ";
}
alert(print);
}

jQuery time clock counter

I'm trying to make a simple jQuery Time counter just to count the user working time in my application displaying hours and minutes
HTML:
<body>
<div><input type="text" class="time"/></div>
</body>
JS:
function startTimer(duration) {
var timer = duration,hours, minutes;
setInterval(function () {
hours = parseInt(timer / 3600, 10)
minutes = parseInt(timer / 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
$('input[type=text].time').val(hours + ":" + minutes);
if (++timer < 0) {
timer = 0;
}
}, 1);
}
jQuery(function ($) {
var minutes = 0
startTimer(minutes);
});
Fiddle:
http://jsfiddle.net/df773p9m/1856/
The problem is that at this time the counter does not reset when reach 60 minutes and continues to count, although the hours were updated.
Notes: I've set the interval to 1 ms in order to test the operation.
you should use % after the division, otherwise it'll continue
check this updated jsfiddle
EDIT
Sean is right, I have fixed the code, the 24 should be in the % not the division also note that I'm multiplying duration by 60 to get the mins from the seconds
no need for the reset mins line anymore
function startTimer(duration) {
var timer = duration*60,hours, minutes;
setInterval(function () {
hours = parseInt((timer / 3600)%24, 10)
minutes = parseInt((timer / 60)%60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
$('input[type=text].time').val(hours + ":" + minutes);
if (++timer < 0) {
timer = 0;
}
}, 1);
}
jQuery(function ($) {
var minutes = 1339;
startTimer(minutes);
});
http://jsfiddle.net/df773p9m/1859/
If your timer is in minutes, and it's counting up one minute at a time, you want to divide the total time by 60 to get hours. Then you can get the remainder with the modulo operator % for the remaining minutes:
function startTimer(duration, display) {
var timer = duration,
hours,
minutes;
setInterval(function () {
hours = parseInt(timer / 60, 10);
minutes = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
$('#time').val(hours + ":" + minutes);
// I have no idea what you're trying to do here, it appears this will never evaluate to true:
if (++timer < 0) {
timer = duration;
}
}, 100); // this number should be changed to 60000 for it to be one minute (60,000 milliseconds = 1 minute)
}
jQuery(function ($) {
var minutes = 0,
display;
startTimer(minutes, display);
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()
Remainder (%)
The remainder operator returns the remainder left over when one
operand is divided by a second operand. It always takes the sign of
the dividend, not the divisor. It uses a built-in modulo function to
produce the result, which is the integer remainder of dividing var1 by
var2 — for example — var1 modulo var2. There is a proposal to get an
actual modulo operator in a future version of ECMAScript, the
difference being that the modulo operator result would take the sign
of the divisor, not the dividend.

loop value in format time(hour and minute)

How to display the value, using for loop in JavaScript?
ex: if i loop number 0 < 2 then i get result like this:
0
1
my question how i make the loop like this:
0:0
0:1
0:2
0:3
0:4
to
0:59
1:0
1:1
1:2
1:3
1:4
to
1:59
because the last loop is 1,how i make +1 so the last loop result like this:
0:0
0:1
0:2
0:3
0:4
to
0:59
1:0
1:1
1:2
1:3
1:4
to
1:59
2:0
thank you
There are (at least) a couple of ways of doing this.
Perhaps the easiest would be to have two nested loops, one for the minute and one for the second, along the lines of the following pseudo-code:
for minutes = 0 to 59 inclusive:
for seconds = 0 to 59 inclusive:
output minutes ":" seconds
That won't necessarily scale well as you move up to hours and days and it means you have to work on a minute-boundary. In other words, you can't easily do 752 seconds (12m32s) unless you do some monstrosity like:
for minutes = 0 to 11 inclusive:
for seconds = 0 to 59 inclusive:
output minutes ":" seconds
for seconds = 0 to 32 inclusive:
output "12:" seconds
Perhaps a better way, which alleviates those problems,would be to work exclusively with seconds and translate that to more complete units on the fly:
for fullsecs = 0 to 752 inclusive:
secsleft = fullsecs
seconds = fullsecs % 60
secsleft = secsleft - seconds
minutes = (secsleft / 60) % 60
secsleft = secsleft - minutes * 60
hours = (secsleft / 3600) % 24
secsleft = secsleft - hours * 3600
days = secsleft / 86400
output days ":" hours ":" minutes ":" seconds
What that snippet within the loop does is to strip out the "odd" seconds (those that aren't a multiple of a minute) and then subtract those seconds from the value, so that the result is a multiple of a minute.
Then it does that, for other units as well, all the way up to days, using the fact that there are 60 seconds in a minute, 60 * 60 = 3600 in an hour, and 24 * 60 * 60 = 86400 in a day.

Javascript converting second to minutes [duplicate]

This question already has answers here:
How to perform an integer division, and separately get the remainder, in JavaScript?
(18 answers)
Closed 7 years ago.
So far I have been able to find the sum of min and sec store in array
var time = ["13:24", "4:28", "7:29"];
var min = 0;
var sec = 0;
for (k in time){
min += +time[k].split(":")[0];
sec += +time[k].split(":")[1];
}
var rem = sec % 60;
min += rem;
alert(min+'-'+sec); //25-81
my desired output it 25-21
I think the desired o/p is 25-21
var time = ["13:24", "4:28", "7:29"];
var min = 0;
var sec = 0;
var minsec = time.forEach(function(time) {
var parts = time.split(":")
min += +parts[0];
sec += +parts[1];
});
//Add the whole minutes from the seconds ie if seconds is 130 then 2 minuste to be added to min
min += Math.floor(sec / 60);
//then the rest 10 secs to be added to sec
sec = sec % 60;
alert(min + '-' + sec);
Your sum is wrong. You're adding the modulo of sec to min. That means if you were on 59 seconds, you'd add 59 minutes to your sum.
Instead you should add the division of sec, and set sec to the modulo:
min += Math.floor(sec / 60);
sec %= 60;
This way 69 seconds would translate to 1 minute and 9 seconds, whereas your current code would compute 9 minutes and 69 seconds.

Categories

Resources