index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _index = _interopRequireDefault(require("../../addLeadingZeros/index.js"));
  8. /*
  9. * | | Unit | | Unit |
  10. * |-----|--------------------------------|-----|--------------------------------|
  11. * | a | AM, PM | A* | |
  12. * | d | Day of month | D | |
  13. * | h | Hour [1-12] | H | Hour [0-23] |
  14. * | m | Minute | M | Month |
  15. * | s | Second | S | Fraction of second |
  16. * | y | Year (abs) | Y | |
  17. *
  18. * Letters marked by * are not implemented but reserved by Unicode standard.
  19. */
  20. var formatters = {
  21. // Year
  22. y: function y(date, token) {
  23. // From http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_tokens
  24. // | Year | y | yy | yyy | yyyy | yyyyy |
  25. // |----------|-------|----|-------|-------|-------|
  26. // | AD 1 | 1 | 01 | 001 | 0001 | 00001 |
  27. // | AD 12 | 12 | 12 | 012 | 0012 | 00012 |
  28. // | AD 123 | 123 | 23 | 123 | 0123 | 00123 |
  29. // | AD 1234 | 1234 | 34 | 1234 | 1234 | 01234 |
  30. // | AD 12345 | 12345 | 45 | 12345 | 12345 | 12345 |
  31. var signedYear = date.getUTCFullYear();
  32. // Returns 1 for 1 BC (which is year 0 in JavaScript)
  33. var year = signedYear > 0 ? signedYear : 1 - signedYear;
  34. return (0, _index.default)(token === 'yy' ? year % 100 : year, token.length);
  35. },
  36. // Month
  37. M: function M(date, token) {
  38. var month = date.getUTCMonth();
  39. return token === 'M' ? String(month + 1) : (0, _index.default)(month + 1, 2);
  40. },
  41. // Day of the month
  42. d: function d(date, token) {
  43. return (0, _index.default)(date.getUTCDate(), token.length);
  44. },
  45. // AM or PM
  46. a: function a(date, token) {
  47. var dayPeriodEnumValue = date.getUTCHours() / 12 >= 1 ? 'pm' : 'am';
  48. switch (token) {
  49. case 'a':
  50. case 'aa':
  51. return dayPeriodEnumValue.toUpperCase();
  52. case 'aaa':
  53. return dayPeriodEnumValue;
  54. case 'aaaaa':
  55. return dayPeriodEnumValue[0];
  56. case 'aaaa':
  57. default:
  58. return dayPeriodEnumValue === 'am' ? 'a.m.' : 'p.m.';
  59. }
  60. },
  61. // Hour [1-12]
  62. h: function h(date, token) {
  63. return (0, _index.default)(date.getUTCHours() % 12 || 12, token.length);
  64. },
  65. // Hour [0-23]
  66. H: function H(date, token) {
  67. return (0, _index.default)(date.getUTCHours(), token.length);
  68. },
  69. // Minute
  70. m: function m(date, token) {
  71. return (0, _index.default)(date.getUTCMinutes(), token.length);
  72. },
  73. // Second
  74. s: function s(date, token) {
  75. return (0, _index.default)(date.getUTCSeconds(), token.length);
  76. },
  77. // Fraction of second
  78. S: function S(date, token) {
  79. var numberOfDigits = token.length;
  80. var milliseconds = date.getUTCMilliseconds();
  81. var fractionalSeconds = Math.floor(milliseconds * Math.pow(10, numberOfDigits - 3));
  82. return (0, _index.default)(fractionalSeconds, token.length);
  83. }
  84. };
  85. var _default = formatters;
  86. exports.default = _default;
  87. module.exports = exports.default;