UNPKG

tdelta

Version:

Date extention libirary for time difference.

270 lines (269 loc) 7.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Delta = void 0; /** * Provideds functionality for duration/difference in times. * */ var Delta = /** @class */ (function () { /** * @param { number } durationInMS The duration/difference in time. */ function Delta(durationInMS) { this.durationInMS = durationInMS; } Delta.diff = function (timeA, timeB) { if (timeA instanceof Date) { timeA = timeA.getTime(); } if (timeB instanceof Date) { timeB = timeB.getTime(); } var diff = timeA - timeB; var milliseconds = Math.abs(diff); return new Delta(milliseconds); }; Object.defineProperty(Delta.prototype, "ms", { /** * Shorthand for `milliseconds`. * * This is not to be mistaken with total milliseconds. * @example * ``` javascript * const ms = 13493; * let dt = Delta(ms); * dt.ms // equals 493 not 13493 * ``` * * @returns { number } The number of milliseconds. */ get: function () { return this.milliseconds; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "milliseconds", { /** * Returns the milliseconds segment of the delta. * * This is not to be mistaken with total milliseconds. * @example * ``` javascript * const ms = 13493; * let dt = Delta(ms); * dt.milliseconds // equals 493 not 13493 * ``` * * @returns { number } The number of milliseconds. */ get: function () { return Math.floor(this.durationInMS) % 1000; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "sec", { /** * Shorthand for `seconds`. * * This is not to be mistaken with total seconds. * @example * ``` javascript * const ms = 152000; * let dt = Delta(ms); * dt.sec // equals 32 not 152 * ``` * * @returns { number } The number of seconds. */ get: function () { return this.seconds; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "seconds", { /** * Returns the seconds segment of the delta. * * This is not to be mistaken with total seconds. * @example * ``` javascript * const ms = 152000; * let dt = Delta(ms); * dt.seconds // equals 32 not 152 * ``` * * @returns { number } The number of seconds. */ get: function () { return Math.floor(this.durationInMS / 1000) % 60; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "min", { /** * Shorthand for `minutes`. * * This is not to be mistaken with total minutes. * @example * ``` javascript * const ms = 9120000; * let dt = Delta(ms); * dt.min // equals 32 not 152 * ``` * * @returns { number } The number of minutes. */ get: function () { return this.minutes; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "minutes", { /** * Returns the minutes segment of the delta. * * This is not to be mistaken with total minutes. * @example * ``` javascript * const ms = 9120000; * let dt = Delta(ms); * dt.minutes // equals 32 not 152 * ``` * * @returns { number } The number of minutes. */ get: function () { return Math.floor(this.durationInMS / 1000 / 60) % 60; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "h", { /** * Shorthand for `hours`. * * This is not to be mistaken with total hours. * @example * ``` javascript * const ms = 216000000; * let dt = Delta(ms); * dt.h // equals 12 not 60 * ``` * * @returns { number } The number of hours. */ get: function () { return this.hours; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "hours", { /** * Returns the hours segment of the delta. * * This is not to be mistaken with total hours. * @example * ``` javascript * const ms = 216000000; * let dt = Delta(ms); * dt.hours // equals 12 not 60 * ``` * * @returns { number } The number of hours. */ get: function () { return Math.floor(this.durationInMS / 1000 / 60 / 60) % 24; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "d", { /** * Shorthand for `days`. * * This is not to be mistaken with total days. * @example * ``` javascript * const ms = 1382400000; * let dt = Delta(ms); * dt.d // equals 2 not 16 * ``` * * @returns { number } The number of days. */ get: function () { return this.days; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "days", { /** * Returns the days segment of the delta. * * This is not to be mistaken with total days. * @example * ``` javascript * const ms = 1382400000; * let dt = Delta(ms); * dt.days // equals 2 not 16 * ``` * * @returns { number } The number of days. */ get: function () { return Math.floor(this.durationInMS / 1000 / 60 / 60 / 24) % 7; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "w", { /** * Shorthand for `weeks`. * * This is equavilant to total weeks. * @example * ``` javascript * const ms = 33868800000; * let dt = Delta(ms); * dt.weeks() // equals 56 not 4 * ``` * * @returns { number } The number of weeks. */ get: function () { return this.weeks; }, enumerable: false, configurable: true }); Object.defineProperty(Delta.prototype, "weeks", { /** * Returns the week segment of the delta. * * This is equavilant to total weeks. * @example * ``` javascript * const ms = 33868800000; * let dt = Delta(ms); * dt.getWeeks() // equals 56 not 4 * ``` * * @returns { number } The number of weeks. */ get: function () { return Math.floor(this.durationInMS / 1000 / 60 / 60 / 24 / 7); }, enumerable: false, configurable: true }); return Delta; }()); exports.Delta = Delta; exports.default = Delta;