Unable to convert seconds to HH:MM:SS - javascript

While trying to convert seconds into HH:MM:SS seconds format, I am getting the incorrect result. For example, if take value of seconds be 289 the minutes come out as 0. What could be the reason for this
var seconds = this.currentTime;
var hours = Math.floor(seconds/3600); // Get whole hours
seconds -= hours*3600;
var minutes = Math.floor(seconds/3600); // Get remaining minutes
console.log("minutes : " + minutes);
seconds -= minutes*60;
var timeNow = hours + ":" + (minutes<10 ? '0'+minutes : minutes) + ":" + (seconds<10 ? '0'+seconds : seconds);

To calculate minutes, you should divide seconds by 60, not 3600 (there are 60 seconds in a minute after all):
var minutes = Math.floor(seconds/60); // Get remaining minutes
You could also use the Date object in JavaScript to do most of this for you:
var date = new Date(0, 0, 0, 0, 0, 289, 0);
var minutes = date.getMinutes();
// etc.

var timeInSec = this.currentTime;
var hours = Math.floor(timeInSec / 3600) % 24; // Get whole hours
var minutes = Math.floor(timeInSec / 60) % 60; // Get remaining minutes
var seconds = timeInSec % 60; // Get remaining seconds
var timeNow = (hours < 10 ? "0" + hours : hours) + "-" + (minutes < 10 ? "0" + minutes : minutes) + "-" + (seconds < 10 ? "0" + seconds : seconds);

Related

Convert hours into minutes

I want to convert the hours into minutes.
Example: If hour is 2:18, then I want the output as 138 minutes.
<script>
m = diff % 60;
h = (diff - m) / 60;
mins = h.toString() + ":" + (m < 10 ? "0" : "") + m.toString();
alert(mins)
</script>
You can easily convert this into javascript. Following code might help you
var hms = '2:18'; // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var minutes= (+a[0]) * 60 + (+a[1]) ;
console.log(minutes);

How can we make the minutes value starts from zero after seconds reached to 60?

I am working on project which need to show some delay time in the format of "hh:mm:ss". When the seconds value reached to 60(means minutes value is 1), then the seconds value again starts from zero. But, when the minutes value reached to 60(means hours value is 1), then the minutes is showing the conitnuous value(61,62,63.....). I need to show the minutes value starts from zero when the hours value is greater than zero. How can I acheive this? Please help me and thanks in advance. Below the code which I have written.
var timer = 0;
setInterval(function () {
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt(timer / 60, 10);
var seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
if (++timer < 0) {
$scope.delayTime = false;
}
}, 1000);
Just change your calc on minutes to (timer / 60) % 60:
var timer = 3600 - 5;
setInterval(function () {
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt((timer / 60) % 60, 10);
var seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
//$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
console.log('hours ' + hours + ' minutes ' + minutes + ' seconds ' + seconds);
if (++timer < 0) {
//$scope.delayTime = false;
}
}, 1000);
Have you tried modulo? Like
59 % 60 -> 59
60 % 60 -> 0
61 % 60 -> 1
62 % 60 -> 2
...
Just add minutes = minutes % 60.
Subtract the minutes already contained in the hours to properly calculate the minutes:
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt(timer / 60 - hours * 60, 10);
var seconds = parseInt(timer % 60, 10);
Actually, your minutes variable contains your whole time in minutes (containing hours):
var minutes = parseInt((timer / 60) % 60, 10);
Here's something I want to add to rasmeister's answer:
Since many browsers (especially mobile browsers) pause javascript when the browser tab is inactive, I recommend you to use Date objects:
var start = +new Date(); // casting Date to number results in timestamp
setInterval(function () {
// get elapsed time in seconds
var timer = Math.round((+new Date() - start) / 1000);
var hours = parseInt(timer / 3600, 10);
var minutes = parseInt((timer / 60) % 60, 10);
var seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
//$scope.delay = $scope.delayTime.label + ": " + hours + ':' + minutes + ':' + seconds;
console.log('hours ' + hours + ' minutes ' + minutes + ' seconds ' + seconds);
if (timer < 0) {
//$scope.delayTime = false;
}
}, 1000);

Increment time interval by 15 minutes in javascript

As stated in the example below, I would like to create an array that is incremented with 15 minute interval.. Irrespective of the getTime() , the array should start from 12.00 AM until 11.45 P.M. Example:
[12.00 AM, 12.15 AM, 12.45 AM, 1.00 AM ... 11.45 P.M.]
Thank you for looking. I found the solution of what I needed.
var hours, minutes, ampm;
var time = [];
for(var i = 0; i <= 1440; i += 15){
hours = Math.floor(i / 60);
minutes = i % 60;
if (minutes < 10){
minutes = '0' + minutes; // adding leading zero
}
ampm = hours % 24 < 12 ? 'AM' : 'PM';
hours = hours % 12;
if (hours === 0){
hours = 12;
}
time.push(hours + ':' + minutes + ' ' + ampm);
}
document.getElementById("Time").innerText = time ;

JavaScript timer to count down from a certain time for a number of hours

I have created a countdown timer. The problem is, I am wanting it to count down from midnight clock 00:00:00 until clock 17:00:00.
I have made the timer count down starting at 17 hours 00 minutes 00 seconds and it works a treat, but I need a way to take off the time from 00:00:00 to present from the 17 hours.
Here is my JS code
function startTimer(duration, display) {
var start = Date.now(),
diff,
hours,
minutes,
seconds;
function timer() {
// get the number of seconds that have elapsed since
// startTimer() was called
diff = duration - (((Date.now() - start) / 1000) | 0);
// Setting and displaying hours, minutes, seconds
hours = (diff / 360) | 0;
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = hours + ":" + minutes + ":" + seconds;
if (diff <= 0) {
// add one second so that the count down starts at the full duration
// example 17:00:00 not 16:59:59
start = Date.now() + 1000;
}
};
// don't want to wait a full second before the timer starts
timer();
setInterval(timer, 1000);
}
window.onload = function () {
var timeLeft = 3600 * 17,
display = document.querySelector('#time');
startTimer(timeLeft, display);
};
Here is my HTML code:
<div>Order by: <span id="time"></span> for Next Day Delivery.</div>
My thoughts were to get the timeLeft = 3600 * 17 and take off the diff.
Here is your corrected code : it was easier to save the timestamp of 17h of the current day (next day if we are after 17h) and compare it to the current timestamp. And to calculate the number of hours left, you have to divide the number of seconds by 3600 (60*60) and not 360
function startTimer(display) {
var date = new Date();
var h17 = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 17);
if(date.getHours() >= 17) {
h17.setDate(h17.getDate()+1);
}
h17 = h17.getTime();
var diff,
hours,
minutes,
seconds;
function timer() {
diff = (((h17 - Date.now()) / 1000) | 0);
// Setting and displaying hours, minutes, seconds
hours = (diff / 3600) | 0;
minutes = ((diff % 3600) / 60) | 0;
seconds = (diff % 60) | 0;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = hours + ":" + minutes + ":" + seconds;
};
timer();
setInterval(timer, 1000);
}
window.onload = function () {
var timeLeft = 3600 * 17,
display = document.querySelector('#time');
startTimer(display);
};
<div>Order by: <span id="time"></span> for Next Day Delivery.</div>
jsFiddle
PS : I didn't test if the function work correctly after 17h but it should

How to add if statement to return variable on a function

This one must seem simple for most of you, but its not for me so any help would be appreciated. How do I change one of the variables on the return part of my function? Do I just the add variable clock = and add the if statements below like so?
if (ms > than 86400000) {clock = days + " " + hours};
or is it like this?
if (ms > than 3600000) return clock : hours + ":" + minutes.
Here is what I have so far.
if (DateDiff("ms", now, TestDue) >= 0) {
var Date1 = new Date();
var Date2 = Date.parse(Date1);
var TestLate1 = Date.parse(TestLate);
var TestDue1 = Date.parse(TestDue);
var TestLate2 = convertMS(TestLate1 - Date2).clock;
var TestDue2 = convertMS(TestDue1 - Date2).clock;
var TestDiff = convertMS(TestLate1 - TestDue1).clock;
tto = "../Images/Blue-120-Button.png";
ttt = "Test Start " + TestDue2;
ttm = "../Images/Blue-120-ButtonMouseOver.png";
ttd = "Test will start in " + TestDue2 + ", will be due in " + (TestDiff) + " after that and will be late on " + TestLate + ".";
C3color = "#FFFFFF";
C3Mcolor = "#FFFF00";
ASTRIS = "../Images/BlueTestB.png";
ASTRIS2 = "../Images/BlueTestB2.png";
so that's the part of the function I need to implement different times for.
function convertMS(ms) {
days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
hours = Math.floor((ms % 86400000) / 3600000), // 1 Hour = 3600000 Milliseconds
minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds
if (minutes.toString().length == 1) {
minutes = "0" + minutes
};
if (seconds.toString().length == 1) {
seconds = "0" + seconds
};
//need to change the conditions with if statements for clock//
return {
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
clock: days + " " + hours + ":" + minutes + ":" + seconds
};
}
What about that way:
function convertMS(ms) {
days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
hours = Math.floor((ms % 86400000)/ 3600000), // 1 Hour = 3600000 Milliseconds
minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
seconds = Math.floor(((ms % 360000) % 60000) / 1000); // 1 Second = 1000 Milliseconds
if (minutes.toString().length == 1 ) {minutes = "0" + minutes};
if (seconds.toString().length == 1 ) {seconds = "0" + seconds};
var clock = '';
if(ms > 86400000) {
clock = days + " " + hours;
} else if(ms > 3600000 && ms < 86400000) {
clock = hours + ":" + minutes;
} else if(ms > 60000 && ms < 3600000) {
clock = minutes + ":" + seconds;
} else {
clock = seconds;
}
return {
days : days,
hours : hours,
minutes : minutes,
seconds : seconds,
clock : clock
};
}
I think you're looking for the ternary operator. This might point you in the right direction:
return ms > 86400000 ? days + " " + hours : hours + ":" + minutes
Or something like that. Check here for more, and you should be able to easily adapt it to your code.
Use a separate variable for formatting the time and you can modify it as much as you want before returning the final object.
function convertMS(ms) {
var days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
hours = Math.floor((ms % 86400000)/ 3600000), // 1 Hour = 3600000 Milliseconds
minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds
if (minutes.toString().length == 1 ) {minutes = "0" + minutes}
if (seconds.toString().length == 1 ) {seconds = "0" + seconds}
var clockFormat;
if( days > 0 ) {
clockFormat = days + " " + hours;
}
else if( hours > 0 ) {
clockFormat = hours + ":" + minutes;
}
else {
clockFormat = minutes + ":" + seconds;
}
return {
days : days,
hours : hours,
minutes : minutes,
seconds : seconds,
clock : clockFormat
};
}

Categories