@onesy/date
Version:
Time and date utils library
49 lines (48 loc) • 2.12 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const OnesyDate_1 = __importStar(require("./OnesyDate"));
function In(to = OnesyDate_1.default.onesyDate, withPrefix = true, from = OnesyDate_1.default.onesyDate) {
if (to.valid &&
from.valid &&
to.milliseconds > from.milliseconds) {
let value = '';
const ins = {
milliseconds: to.milliseconds - from.milliseconds,
};
ins['second'] = Math.floor(ins['milliseconds'] / 1e3);
ins['minute'] = Math.floor(ins['second'] / 6e1);
ins['hour'] = Math.floor(ins['minute'] / 6e1);
ins['day'] = Math.floor(ins['hour'] / 24);
ins['month'] = Math.floor(ins['day'] / 30);
ins['year'] = Math.floor(ins['month'] / 12);
OnesyDate_1.units.forEach(unit => {
if (ins[unit] > 0)
value = ins[unit] === 1 ? `a${unit === 'hour' ? 'n' : ''} ${unit}` : `${ins[unit]} ${unit}s`;
if (unit === 'second' && ins[unit] < 14)
value = `a few seconds`;
else if (unit === 'millisecond' && ins[unit] < 14)
value = `a few seconds`;
});
return `${withPrefix ? 'in ' : ''}${value}`;
}
}
exports.default = In;