UNPKG

812 BJavaScriptView Raw
1'use strict';
2
3function ago(since) {
4 const strftime = require('strftime');
5 let elapsed = Math.floor((new Date() - since) / 1000);
6 let message = strftime('%Y/%m/%d %H:%M:%S %z', since);
7 if (elapsed < 60) return `${message} (~ ${Math.floor(elapsed)}s ago)`;else if (elapsed < 60 * 60) return `${message} (~ ${Math.floor(elapsed / 60)}m ago)`;else if (elapsed < 60 * 60 * 25) return `${message} (~ ${Math.floor(elapsed / 60 / 60)}h ago)`;else return message;
8}
9
10function remaining(from, to) {
11 let secs = Math.floor(to / 1000 - from / 1000);
12 let mins = Math.floor(secs / 60);
13 let hours = Math.floor(mins / 60);
14 if (hours > 0) return `${hours}h ${mins % 60}m`;
15 if (mins > 0) return `${mins}m ${secs % 60}s`;
16 if (secs > 0) return `${secs}s`;
17 return '';
18}
19
20module.exports = {
21 ago,
22 remaining
23};
\No newline at end of file