Setting a countdown timer and having problems with IE - javascript

I've set up a countdown timer with 1 sec interval and increment / decrement in mill secs..
I then searched for something that would give me the value in minutes/seconds. I came up with the following:
var timer = 130000;
var mins = Math.floor((timer % 36e5) / 6e4),
secs = Math.floor((timer % 6e4) / 1000);
The above code works on Safari, Chrome and Firefox with no problem. When I get to Internet Explorer, it doesn't work at all.
Is there another way of doing it that would work on all browsers?

Try with removing the exponetial.
var timer = 130000;
var mins = Math.floor((timer % 3600000) / 60000),
secs = Math.floor((timer % 60000) / 1000);
Read more about Exponential Notation.
An Exponential Notation if number with format a x 10^n, where 1<= a < 10 and n is integer with positive or negative value.
For example:
36e5
= 36 x 10^5
= 36 x 100000
= 3600000
ans so on.

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".

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.

How to multiply time using javascript?

I have the following timespan coming from a model in MVC:
timeTaken = "00:01:00";
Then I have a multiplier
multiply = "3";
Result: 00:03:00
What would be the best way to calculate this time?
I don't know a great deal of libraries. I was thinking of splitting the seconds, minutes and hours, dividing each one into seconds, multiplying then putting it back together.
However, I have this kind of calculations for many sections, it just seems a little mundane. Can I just multiply the time in a better manner?
Thanks
I am combining the snippets I found in multiple pages. Conversion of hh:mm:ss to seconds, multiply 3x and then again convert to hh:mm:ss.
var hms = '00:01:00'; // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var newSeconds= 3*seconds;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(newSeconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);
JSFiddle
First you can convert them to seconds as below
var hms = "00:01:00";
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var newSeconds=seconds * 3;
var t = new Date();
t.setSeconds(newSeconds);
console.log(t);
DEMO
Update
To just obtain time do as below
var time=t.toTimeString().split(' ')[0]
DEMO
UPDATE
To obtain just hour from time you can do as follows
t.toTimeString().split(' ')[0].split(':')[0]
and to obtain hour in 12 hour format you can do as below:
var hour;
if(t.toTimeString().split(' ')[0].split(':')[0]>12)
hour=t.toTimeString().split(' ')[0].split(':')[0]-12;
else
hour=t.toTimeString().split(' ')[0].split(':')[0];
alert(hour);
UPDATED DEMO

How to turn a decimaled number into a framerate designation in javascript

I am using the flowplayer video player and a flowplayer function is giving me a decimal for the point on the player timeline. i.e. Instead of giving me a traditional:
00:00:01:03
timestamp, it just gives me
1.03333
or the equivalent. The
.033333
essentially serves as the frames although I believe it is, in that case based on a 10 fps framerate (which is fine for what I am doing.)
I am not skilled at all in working with numbers/decimals in JS. How can I convert that
1.033333
into a timestamp like
00:00:01:03?
Use division and modulus: x % 60 will give you the remainder of dividing by 60 (363 % 60 is 3), then floor divide (Math.floor(363 / 60) is 6) and use modulus again, repeat.
So:
var timestamp = 1.03333;
var seconds = timestamp % 60;
timestamp = Math.floor(timestamp / 60);
var minutes = timestamp % 60;
timestamp = Math.floor(timestamp / 60);
var hours = timestamp;

Most Efficient/Short Way to take X seconds and turn it into h:m:s

I'm looking to turn 165 seconds into 2:40 not 0:2:45
The function needs to be able to adapt to how large the seconds value is.
I know there's infinite ways to do this, but I'm looking for a clean way to do it without any external libraries other than jQuery.
Something like: [Math.floor(165/60),165%60].join(':') should work. Actually, it's 2:45 ;~)
[edit] based on your comment a function to convert seconds into a (zero padded, hours trimmed) hr:mi:se string
function hms(sec){
var hr = parseInt(sec/(60*60),10)
, mi = parseInt(sec/60,10)- (hr*60)
, se = sec%60;
return [hr,mi,se]
.join(':')
.replace(/\b\d\b/g,
function(a){
return Number(a)===0 ? '00' : a<10? '0'+a : a;
}
)
.replace(/^00:/,'');
}
alert(hms(165)); //=> 02:45
alert(hms(3850)); //=> 01:04:10
Check this answer : Convert seconds to HH-MM-SS with JavaScript?
hours = totalSeconds / 3600;
totalSeconds %= 3600;
minutes = totalSeconds / 60;
seconds = totalSeconds % 60;
Try something like this (i've included padding to format the digits to two characters each):
String.prototype.padLeft = function(n, pad)
{
t = '';
if (n > this.length){
for (i = 0; i < n - this.length; i++) {
t += pad;
}
}
return t + this;
}
var seconds = 3850;
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor(seconds % 3600 / 60);
var time = [hours.toString().padLeft(2, '0'),
minutes.toString().padLeft(2, '0'),
(seconds % 60).toString().padLeft(2, '0')].join(':');

Categories

Resources