index.js 1.8 KB

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