I have a Unix timestamp that comes back from the server when an item is created and my goal is to 'expire' the item after 24 hours.. I am trying to make a countdown function that will convert the Unix timestamp into HH:MM:SS format and countdown from 24 hours (Current browser time - the unix timestamp converted).
Asked
Active
Viewed 1,044 times
0
-
1_"I am trying to make a countdown function"_ that's nice. Are you having trouble with any particular part? – Phil Mar 20 '19 at 01:19
1 Answers
1
I think this is what you are looking for:
String.prototype.toHHMMSS = function() {
var sec_num = parseInt(this, 10);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
return hours + ":" + minutes + ":" + seconds;
};
let startTime = ((new Date()).getTime() / 1000) + 86400; // database unix-timestamp value
setInterval(() => {
let curTime = (new Date()).getTime() / 1000;
document.getElementById("timer").innerText = (`${startTime-curTime}`).toHHMMSS();
}, 1000);
<div id="timer"></div>
Hope this helps,

Miroslav Glamuzina
- 4,472
- 2
- 19
- 33