UNPKG

@beenotung/tslib

Version:
56 lines (55 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTimestamp = getTimestamp; exports.d2 = d2; exports.d3 = d3; /** * return in below formats: * * | Resolution | Format | * |-----------:|:-------| * | date | YYYY-MM-DD | * | minute | YYYY-MM-DD HH:MM | * | second | YYYY-MM-DD HH:MM:SS | * | millisecond| YYYY-MM-DD HH:MM:SS.sss | */ function getTimestamp(time = Date.now()) { const resolution = getTimestamp.resolution; const date = new Date(time); const year = date.getFullYear(); const month = d2(date.getMonth() + 1); const day = d2(date.getDate()); if (resolution === 'date') { return `${year}-${month}-${day}`; } const hour = d2(date.getHours()); const minute = d2(date.getMinutes()); if (resolution === 'minute') { return `${year}-${month}-${day} ${hour}:${minute}`; } const second = d2(date.getSeconds()); if (resolution === 'second') { return `${year}-${month}-${day} ${hour}:${minute}:${second}`; } const millisecond = d3(date.getMilliseconds()); return `${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}`; } (function (getTimestamp) { // eslint-disable-next-line prefer-const getTimestamp.resolution = 'second'; })(getTimestamp || (exports.getTimestamp = getTimestamp = {})); function d2(n) { if (n < 10) { return '0' + n; } return n.toString(); } function d3(n) { if (n < 10) { return '00' + n; } if (n < 100) { return '0' + n; } return n.toString(); }