index.js 1.3 KB

123456789101112131415161718192021222324
  1. "use strict";
  2. ///<reference path="index.d.ts"/>
  3. function prependZero(matched, num) {
  4. return matched.length > 1 && num < 10 ? "0" + num : "" + num;
  5. }
  6. Date.prototype.format = function (pattern) {
  7. var _this = this;
  8. if (pattern === void 0) { pattern = 'YYYY-MM-DD hh:mm:ss'; }
  9. return pattern.replace(/y{2,}|Y{2,}/, function (v) { return (_this.getFullYear() + "").substr(4 - v.length); })
  10. .replace(/M{1,2}/, function (v) { return prependZero(v, _this.getMonth() + 1); })
  11. .replace(/D{1,2}|d{1,2}/, function (v) { return prependZero(v, _this.getDate()); })
  12. .replace(/Q|q/, function (v) { return prependZero(v, Math.ceil((_this.getMonth() + 1) / 3)); })
  13. .replace(/h{1,2}|H{1,2}/, function (v) { return prependZero(v, _this.getHours()); })
  14. .replace(/m{1,2}/, function (v) { return prependZero(v, _this.getMinutes()); })
  15. .replace(/s{1,2}/, function (v) { return prependZero(v, _this.getSeconds()); })
  16. .replace(/SSS|S/, function (v) {
  17. var ms = '' + _this.getMilliseconds();
  18. return v.length === 1 ? ms : "" + (ms.length === 1 ? '00' : ms.length === 2 ? '0' : '') + ms;
  19. });
  20. };
  21. Date.today = function () {
  22. var now = new Date();
  23. return new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
  24. };