index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { FORMAT_DEFAULT } from '../../constant';
  2. export default (function (o, c) {
  3. // locale needed later
  4. var proto = c.prototype;
  5. var oldFormat = proto.format;
  6. proto.format = function (formatStr) {
  7. var _this = this;
  8. var locale = this.$locale();
  9. if (!this.isValid()) {
  10. return oldFormat.bind(this)(formatStr);
  11. }
  12. var utils = this.$utils();
  13. var str = formatStr || FORMAT_DEFAULT;
  14. var result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, function (match) {
  15. switch (match) {
  16. case 'Q':
  17. return Math.ceil((_this.$M + 1) / 3);
  18. case 'Do':
  19. return locale.ordinal(_this.$D);
  20. case 'gggg':
  21. return _this.weekYear();
  22. case 'GGGG':
  23. return _this.isoWeekYear();
  24. case 'wo':
  25. return locale.ordinal(_this.week(), 'W');
  26. // W for week
  27. case 'w':
  28. case 'ww':
  29. return utils.s(_this.week(), match === 'w' ? 1 : 2, '0');
  30. case 'W':
  31. case 'WW':
  32. return utils.s(_this.isoWeek(), match === 'W' ? 1 : 2, '0');
  33. case 'k':
  34. case 'kk':
  35. return utils.s(String(_this.$H === 0 ? 24 : _this.$H), match === 'k' ? 1 : 2, '0');
  36. case 'X':
  37. return Math.floor(_this.$d.getTime() / 1000);
  38. case 'x':
  39. return _this.$d.getTime();
  40. case 'z':
  41. return "[" + _this.offsetName() + "]";
  42. case 'zzz':
  43. return "[" + _this.offsetName('long') + "]";
  44. default:
  45. return match;
  46. }
  47. });
  48. return oldFormat.bind(this)(result);
  49. };
  50. });