Public
Edited
Feb 3, 2023
Insert cell
# Describe the Time
Insert cell
test = 1227930000;
//"1 day and 1 hour ago"
Insert cell
solution = {
let ms = test;
let output = new Array();
// Valid units to use are minute, hour, day, and week
const units = [60000, 3600000, 86400000, 604800000].reverse();
let calc = 0;
// Weeks
calc = Math.floor(ms / units[0]);
if (calc > 0) {
if (calc == 1) {
output.push("1 week")
}
else {
output.push(calc + " weeks");
}
}

// Days
ms = ms - calc * units[0];
calc = Math.floor(ms / units[1]);
if (calc > 0) {
if (calc == 1) {
output.push("1 day")
}
else {
output.push(calc + " days");
}
}

// Hours
ms = ms - calc * units[1];
calc = Math.floor(ms / units[2]);
if (calc > 0) {
if (calc == 1) {
output.push("1 hour")
}
else {
output.push(calc + " hours");
}
}
// Minutes
ms = ms - calc * units[2];
calc = Math.floor(ms / units[3]);
if (calc > 0) {
if (calc == 1) {
output.push("1 minute")
}
else {
output.push(calc + " minutes");
}
}

if (output.length == 3) {
output.splice(2, 0, "and");
output.splice(1, 0, ",");
}
if (output.length == 2) output.splice(1, 0, "and");
if (output.length == 0) return "Just Now";

output.push("ago");
return output.join(" ");
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more