loop value in format time(hour and minute) - javascript

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.

Related

How can I understand what this javascript timer means in detail?

I created a countdown / timer a few weeks ago using Javascript. The code is also executed correctly by the browser.
Today I looked at the code again and I make notes (Javascript comments) to understand the code and what exactly it does and to better understand Javascrpit.
I'm stuck at the moment. Here's a small piece of code that I absolutely don't understand.
What does the modulo operator do with time? Seconds, minutes, hours...
What exactly does y do?
and why are tenary operators used?
I would be very grateful if someone could explain to me in their own words what exactly the code does. thanks
function timer() {
let seconds = count % 60;
let minutes = Math.floor(count / 60);
let hours = Math.floor(minutes / 60);
minutes %= 60;
hours %= 60;
y = ((minutes>0) ? ((minutes>9) ? minutes : '0'+minutes) + ":" : "")
y += (seconds>9 || minutes == 0) ? seconds : '0'+seconds;
Same Code with my Comments :)
function timer() {
// SET VARIABLE FOR SECONDS = DONT KKNOW WHAT count % 60 means ???
let seconds = count % 60;
// SET VARIABLE FOR MINUTES = DONT KKNOW WHAT Math.floor(count / 60) means ???
let minutes = Math.floor(count / 60);
// SET VARIABLE FOR MINUTES = DONT KKNOW WHAT Math.floor(minutes / 60) ???
let hours = Math.floor(minutes / 60);
// WHY USING %= OPERATER ???
minutes %= 60;
hours %= 60;
// DONT UNDERSTAND Y ??? WHY USING TENARY OPERATORS ???
y = ((minutes>0) ? ((minutes>9) ? minutes : '0'+minutes) + ":" : "")
y += (seconds>9 || minutes == 0) ? seconds : '0'+seconds;
EDIT: count = 3600 SECONDS
You don't explain what count is, but it would appear to be a duration in seconds.
The modulo operator and the floor(a/b) operations are being used to convert the duration in seconds into a base-60 (i. e. Sumerian) representation, i. e., in hours, minutes, and seconds.
y is being built up to show the hours, minutes, and seconds as two decimal digits each, separated with colons, as is conventional to represent a time duration in hours, minutes, and seconds. So, for example, the final value might be "6:01:02" for six hours, one minute, and two seconds. For each base-sixty "digit", we want two decimal digits. The normal conversion of numbers to decimal notation does not include any leading zeros. If the answer were to have only one decimal digit, we have to append one leading zero to the beginning. So, for example, for 8, we would like to see "08".

Is my modulo use wrong or is there a Math.floor rounding error happening with my seconds to minutes calculation?

SOLVED: Seems it's a problem that only exists in Codepen. Running code in a different environment didn't reproduce the problem.
Am writing code to split seconds into years, days, minutes, seconds. I'm getting some unexpected results: eg 3600 seconds comes out as 1 hour. But 3599 seconds comes out as 60 minutes and 59 seconds.
Firstly, there should never be 60 minutes. This should be 1 hour. But that aside the actual result is wrong.
Is there some error in my calculation logic, or is there a funky rounding error going on using modulo and Math.floor? Thanks!
function convertFromSeconds(time) {
const minute = 60
const hour = 60 * minute; //3600
const day = 24 * hour; // 86400
const year = 365 * day //31536000
const years = Math.floor(time / year)
const days = Math.floor((time % year) / day)
const hours = Math.floor((time % day) / hour)
const minutes = Math.floor((time % hour) / minute)
const seconds = time % minute;
// Rest of code formats the above result into a string for output
}
function convertFromSeconds(time) {
const minute = 60
const hour = 60 * minute; //3600
const day = 24 * hour; // 86400
const year = 365 * day //31536000
const years = Math.floor(time / year)
const days = Math.floor((time % year) / day)
const hours = Math.floor((time % day) / hour)
const minutes = Math.floor((time % hour) / minute)
const seconds = time % minute;
// Rest of code formats the above result into a string for output
return { years, days, hours, minutes, seconds }
}
console.log('3600:', convertFromSeconds(3600))
console.log('3599:', convertFromSeconds(3599))

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);
}

Using moment js to round a given number of minutes

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))

How to add 1 hour and 5 minutes in Javascript

I have a requirement to show the pop up of every 1 hour 5 seconds, have set the time for 1 hour, how to set a time for 1 hour 5 seconds using Javascript.
for 3 minutes set the time for 3 * 60 * 1000;
for 1 hour set the time for 1 * 60 * 60 * 1000;
how to be for 1 hour 5 minutes?
Use date object and it's methods. getTime() will get the current date in milliseconds and add appropriate milliseconds for 1 hour 5 minute (which is 65 minute), which will be 65 * 60 * 1000. And after use setTime() to create the date according to the milliseconds.
var date = new Date();
console.log(date);
date.setTime(date.getTime() + 65 * 60 * 1000);
console.log(date);
1 Hr s= (1 * 60 * 60* 1000)
5 Sec = (5*1000)
Then
1 Hrs 5 Sec = (1 * 60 * 60* 1000)+(5*1000)

Categories

Resources