Skip to content

Instantly share code, notes, and snippets.

@afzafri
Created January 20, 2022 04:31
Show Gist options
  • Save afzafri/7b867524520a34a762bd05be0bbc121e to your computer and use it in GitHub Desktop.
Save afzafri/7b867524520a34a762bd05be0bbc121e to your computer and use it in GitHub Desktop.
Calculate total course time for Alibaba Cloud Certificate
var dates = document.getElementsByClassName("date");
var totalTime = '00:00:00';
for(var i=0;i<dates.length;i++) {
totalTime = formatTime(timestrToSec(totalTime) + timestrToSec('00:'+dates[i].innerText));
}
console.log("Total Time: "+totalTime);
function timestrToSec(timestr) {
var parts = timestr.split(":");
return (parts[0] * 3600) +
(parts[1] * 60) +
(+parts[2]);
}
function pad(num) {
if(num < 10) {
return "0" + num;
} else {
return "" + num;
}
}
function formatTime(seconds) {
return [pad(Math.floor(seconds/3600)),
pad(Math.floor(seconds/60)%60),
pad(seconds%60),
].join(":");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment