index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. var utils = this.$utils();
  15. var str = formatStr || FORMAT_DEFAULT;
  16. var result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, function (match) {
  17. switch (match) {
  18. case 'Q':
  19. return Math.ceil((_this.$M + 1) / 3);
  20. case 'Do':
  21. return locale.ordinal(_this.$D);
  22. case 'gggg':
  23. return _this.weekYear();
  24. case 'GGGG':
  25. return _this.isoWeekYear();
  26. case 'wo':
  27. return locale.ordinal(_this.week(), 'W');
  28. // W for week
  29. case 'w':
  30. case 'ww':
  31. return utils.s(_this.week(), match === 'w' ? 1 : 2, '0');
  32. case 'W':
  33. case 'WW':
  34. return utils.s(_this.isoWeek(), match === 'W' ? 1 : 2, '0');
  35. case 'k':
  36. case 'kk':
  37. return utils.s(String(_this.$H === 0 ? 24 : _this.$H), match === 'k' ? 1 : 2, '0');
  38. case 'X':
  39. return Math.floor(_this.$d.getTime() / 1000);
  40. case 'x':
  41. return _this.$d.getTime();
  42. case 'z':
  43. return "[" + _this.offsetName() + "]";
  44. case 'zzz':
  45. return "[" + _this.offsetName('long') + "]";
  46. default:
  47. return match;
  48. }
  49. });
  50. return oldFormat.bind(this)(result);
  51. };
  52. });