UNPKG

typescript-libraries

Version:
60 lines (59 loc) 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TSDate = void 0; var TSString_1 = require("./TSString"); var TSDate = /** @class */ (function () { function TSDate() { } TSDate.utc = function (type, data, set) { if (['Milliseconds', 'Seconds', 'Minutes', 'Hours', 'Date', 'Month', 'FullYear'].indexOf(type) === -1) { throw Error("[TSDate::utc] Type (".concat(type, ") not supported.")); } var date = new Date(); if (set) { date['setUTC' + type].apply(date, set); } date['setUTC' + type](date['getUTC' + type]() + data); return date; }; TSDate.key = function (max, date) { if (max === void 0) { max = 12; } if (date === void 0) { date = new Date(); } return date.toISOString().replace(/[^0-9]/g, '').substring(0, max); }; TSDate.parse = function (date, data, set) { if (data === void 0) { data = 0; } if (set === void 0) { set = [24, 0, 0, 0]; } var dts = Date.parse(date); if (isNaN(dts)) { return TSDate.utc('Hours', data, set).toISOString(); } return new Date(date).toISOString(); }; TSDate.yyyymmddhhss = function (date) { var _a = date.toString().match(/\d{2}/g), y1 = _a[0], y2 = _a[1], m = _a[2], d = _a[3], h = _a[4], mm = _a[5]; return new Date("".concat(y1).concat(y2, "-").concat(m, "-").concat(d, "T").concat(h, ":").concat(mm, ":00.000Z")); }; TSDate.format = function (timestamp, format, cb) { if (timestamp === void 0) { timestamp = Date.now(); } var d = new Date(timestamp), _a = d.toUTCString().split(/[\s+,:]+/), day = _a[0], date = _a[1], month = _a[2], year = _a[3], hours = _a[4], minutes = _a[5], seconds = _a[6], timezone = _a[7], data = { day: day, date: date, month: month, year: year, hours: hours, minutes: minutes, seconds: seconds, timezone: timezone }; data['_'] = d; return TSString_1.TSString.parseAndReplace(format, cb ? cb(data) : data); }; TSDate.timer = function (milliseconds, format, cb) { var data = { days: function (t) { return t / (1000 * 60 * 60 * 24); }, hours: function (t) { return (t / (1000 * 60 * 60)) % 24; }, minutes: function (t) { return (t / 1000 / 60) % 60; }, seconds: function (t) { return (t / 1000) % 60; } }; for (var p in data) { if (data.hasOwnProperty(p)) { data[p] = TSString_1.TSString.pad(Math.floor(data[p](milliseconds)), 2, '0'); } } return TSString_1.TSString.parseAndReplace(format, cb ? cb(data) : data); }; return TSDate; }()); exports.TSDate = TSDate;