k8w-super-date
Version:
Useful functions extended to native Date
25 lines (24 loc) • 1.29 kB
JavaScript
;
///<reference path="index.d.ts"/>
function prependZero(matched, num) {
return matched.length > 1 && num < 10 ? "0" + num : "" + num;
}
Date.prototype.format = function (pattern) {
var _this = this;
if (pattern === void 0) { pattern = 'YYYY-MM-DD hh:mm:ss'; }
return pattern.replace(/y{2,}|Y{2,}/, function (v) { return (_this.getFullYear() + "").substr(4 - v.length); })
.replace(/M{1,2}/, function (v) { return prependZero(v, _this.getMonth() + 1); })
.replace(/D{1,2}|d{1,2}/, function (v) { return prependZero(v, _this.getDate()); })
.replace(/Q|q/, function (v) { return prependZero(v, Math.ceil((_this.getMonth() + 1) / 3)); })
.replace(/h{1,2}|H{1,2}/, function (v) { return prependZero(v, _this.getHours()); })
.replace(/m{1,2}/, function (v) { return prependZero(v, _this.getMinutes()); })
.replace(/s{1,2}/, function (v) { return prependZero(v, _this.getSeconds()); })
.replace(/SSS|S/, function (v) {
var ms = '' + _this.getMilliseconds();
return v.length === 1 ? ms : "" + (ms.length === 1 ? '00' : ms.length === 2 ? '0' : '') + ms;
});
};
Date.today = function () {
var now = new Date();
return new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
};