I converted an integer to a date format but the date object does not accept very large integers. The sample outputs show that the date could go even as far as '96 days, 11 hours, 31 minutes and 35 seconds' but the way I formatted it, only goes as far as 6 days. I tried using the BigInt method on the seconds but the date becomes invalid.
const formatDuration = seconds => {
let str = "";
let date = new Date(0, 0, 0, 0, 0, seconds, 0);
if (seconds == 0) {
str += "now";
}
if (date.getHours() == 0) {
if (date.getDay() > 1) {
str += date.getDay() + " " + "days";
} else if (date.getDay() == 1) {
str += date.getDay() + " " + "day";
}
} else if (date.getDay() > 1) {
str += date.getDay() + " " + "days" + ", ";
} else if (date.getDay() == 1) {
str += date.getDay() + " " + "day" + ", ";
}
if(date.getMinutes() == 0) {
if (date.getHours() > 1) {
str += +date.getHours() + " " + "hours" ;
} else if (date.getHours() == 1) {
str += date.getHours() + " " + "hour";
}
} else if (date.getHours() > 1) {
str += +date.getHours() + " " + "hours" + ", ";
} else if (date.getHours() == 1) {
str += date.getHours() + " " + "hour" + ", ";
}
if (date.getSeconds() == 0) {
if (date.getMinutes() > 1) {
str += date.getMinutes() + " " + "minutes";
} else if (date.getMinutes() == 1) {
str += date.getMinutes() + " " + "minute";
}
} else if (date.getMinutes() > 1) {
str += date.getMinutes() + " " + "minutes" + " and ";
} else if (date.getMinutes() == 1) {
str += date.getMinutes() + " " + "minute" + " and ";
}
if (date.getSeconds() > 1) {
str += date.getSeconds() + " " + "seconds";
} else if (date.getSeconds() == 1) {
str += date.getSeconds() + " " + "second";
}
return str;
}
console.log(formatDuration(3600));
console.log(formatDuration(9829143));