~mikini/hometools

55253720d4d2e7b1b52a2d913f9ba3ca8362f736 — Mikkel Kirkgaard Nielsen 4 months ago e2ad43f
Add time diff utility
1 files changed, 49 insertions(+), 0 deletions(-)

A time_diff.js
A time_diff.js => time_diff.js +49 -0
@@ 0,0 1,49 @@
#! /usr/bin/node

// time diff calculation
// param 1: begin date
// param 2: end date
// param 1: diff unit

var begin=process.argv[2] ? new Date(process.argv[2]) : new Date("2020-05-07 10:43:57.858");
var end=process.argv[3] ? new Date(process.argv[3]) : new Date();
var unit_a=process.argv[4];
var unit_s;
var interval=1; // in milisecond units (Date's resolution)

switch(unit_a)
{
    case "days":
    case "day":
    case "d":
      interval*=24;
      unit_s=unit_s ? unit_s : "days";
    case "hours":
    case "h":
      interval*=60;
      unit_s=unit_s ? unit_s : "hours";
    case "minutes":
    case "mins":
    case "min":
    case "m":
    default:
      interval*=60;
      unit_s=unit_s ? unit_s : "minutes";
    case "seconds":
    case "secs":
    case "sec":
    case "s":
      interval*=1000;
      unit_s=unit_s ? unit_s : "seconds";
    case "miliseconds":
    case "milis":
    case "mili":
    case "ms":
      interval*=1;
      unit_s=unit_s ? unit_s : "miliseconds";
    break;
}

console.log("Begin : " + begin.toISOString());
console.log("End   : " + end.toISOString());
console.log("Diff  : " + (end-begin)/interval + ' ' + unit_s);