Skip to content

Instantly share code, notes, and snippets.

@adithya-badidey
Last active January 25, 2024 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adithya-badidey/995949c381711d4b27845533d38c014c to your computer and use it in GitHub Desktop.
Save adithya-badidey/995949c381711d4b27845533d38c014c to your computer and use it in GitHub Desktop.
Macro to return a human readable time difference between given time and now.
/*\
title: $:/adithyab/macros/timediff.js
type: application/javascript
module-type: macro
Macro to return a human readable time difference between present and given times.
If caltime is false, it will ignore the time part of the date and only work on the date.
Date should be passed in JS compatible format "YYYY-MM-DD". With time, the format should be "YYYY-MM-DD H:MM AM/PM"
Visit https://adithyab.in/2020/how-to-make-a-basic-javascript-macro-in-tiddlywiki/
\*/
(function() {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Information about this macro
*/
exports.name = "timediff";
exports.params = [{
name: "datetime"
}, {
name: "caltime",
default: false
}];
/*
Run the macro
*/
exports.run = function(datetime, caltime) {
try {
var returnstring = "";
var millisec = Date.parse(datetime) - Date.now();
var nosuffixes = false;
caltime = (caltime == 'true');
if (caltime == false) {
var start = new Date();
start.setHours(0, 0, 0, 0);
millisec = Date.parse(datetime) - start;
}
var future = (millisec > 0)
var millisec = Math.abs(millisec)
if (future) {
returnstring = "In ";
}
var seconds = (millisec / 1000);
var minutes = (millisec / (1000 * 60));
var hours = (millisec / (1000 * 60 * 60));
var days = (millisec / (1000 * 60 * 60 * 24));
var months = (millisec / (30.44 * 1000 * 60 * 60 * 24));
var years = (millisec / (365.2422 * 1000 * 60 * 60 * 24));
if (caltime == true) {
if (seconds < 60) {
returnstring = "<mark>NOW</mark>";
nosuffixes = true;
} else if (minutes < 60) {
returnstring += Math.round(minutes) + " Min";
} else if (hours < 24) {
returnstring += Math.round(hours) + " Hr(s)";
} else if (days < 31) {
returnstring += Math.round(days) + " Day(s)";
} else if (months < 12) {
returnstring += Math.round(months) + " Month(s)";
} else {
returnstring += Math.round(years) + " Year(s)";
}
} else {
if (hours < 24) {
if (future == true) {
returnstring = "<mark>_TODAY_</mark>";
} else {
returnstring = "<mark>_Yesterday_</mark>";
}
nosuffixes = true;
} else if (days < 31) {
if (future == true && days < 2) {
returnstring = "<mark>_TOMORROW_</mark>";
nosuffixes = true;
} else {
returnstring += Math.round(days) + " Days";
}
} else if (months < 12) {
returnstring += Math.round(months) + " Month(s)";
} else {
returnstring += Math.round(years) + " Year(s)";
}
}
if ((!future) && (!nosuffixes)) {
returnstring += " ago";
}
return returnstring;
} catch (err) {
console.error(err.stack)
return "(ERROR: " + err.message + ") ";
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment