index.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = formatISO9075;
  7. var _index = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index2 = _interopRequireDefault(require("../isValid/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/addLeadingZeros/index.js"));
  10. /**
  11. * @name formatISO9075
  12. * @category Common Helpers
  13. * @summary Format the date according to the ISO 9075 standard (https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_get-format).
  14. *
  15. * @description
  16. * Return the formatted date string in ISO 9075 format. Options may be passed to control the parts and notations of the date.
  17. *
  18. * @param {Date|Number} date - the original date
  19. * @param {Object} [options] - an object with options.
  20. * @param {'extended'|'basic'} [options.format='extended'] - if 'basic', hide delimiters between date and time values.
  21. * @param {'complete'|'date'|'time'} [options.representation='complete'] - format date, time, or both.
  22. * @returns {String} the formatted date string
  23. * @throws {TypeError} 1 argument required
  24. * @throws {RangeError} `date` must not be Invalid Date
  25. * @throws {RangeError} `options.format` must be 'extended' or 'basic'
  26. * @throws {RangeError} `options.representation` must be 'date', 'time' or 'complete'
  27. *
  28. * @example
  29. * // Represent 18 September 2019 in ISO 9075 format:
  30. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52))
  31. * //=> '2019-09-18 19:00:52'
  32. *
  33. * @example
  34. * // Represent 18 September 2019 in ISO 9075, short format:
  35. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
  36. * //=> '20190918 190052'
  37. *
  38. * @example
  39. * // Represent 18 September 2019 in ISO 9075 format, date only:
  40. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
  41. * //=> '2019-09-18'
  42. *
  43. * @example
  44. * // Represent 18 September 2019 in ISO 9075 format, time only:
  45. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  46. * //=> '19:00:52'
  47. */
  48. function formatISO9075(dirtyDate, options) {
  49. var _options$format, _options$representati;
  50. if (arguments.length < 1) {
  51. throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
  52. }
  53. var originalDate = (0, _index.default)(dirtyDate);
  54. if (!(0, _index2.default)(originalDate)) {
  55. throw new RangeError('Invalid time value');
  56. }
  57. var format = String((_options$format = options === null || options === void 0 ? void 0 : options.format) !== null && _options$format !== void 0 ? _options$format : 'extended');
  58. var representation = String((_options$representati = options === null || options === void 0 ? void 0 : options.representation) !== null && _options$representati !== void 0 ? _options$representati : 'complete');
  59. if (format !== 'extended' && format !== 'basic') {
  60. throw new RangeError("format must be 'extended' or 'basic'");
  61. }
  62. if (representation !== 'date' && representation !== 'time' && representation !== 'complete') {
  63. throw new RangeError("representation must be 'date', 'time', or 'complete'");
  64. }
  65. var result = '';
  66. var dateDelimiter = format === 'extended' ? '-' : '';
  67. var timeDelimiter = format === 'extended' ? ':' : '';
  68. // Representation is either 'date' or 'complete'
  69. if (representation !== 'time') {
  70. var day = (0, _index3.default)(originalDate.getDate(), 2);
  71. var month = (0, _index3.default)(originalDate.getMonth() + 1, 2);
  72. var year = (0, _index3.default)(originalDate.getFullYear(), 4);
  73. // yyyyMMdd or yyyy-MM-dd.
  74. result = "".concat(year).concat(dateDelimiter).concat(month).concat(dateDelimiter).concat(day);
  75. }
  76. // Representation is either 'time' or 'complete'
  77. if (representation !== 'date') {
  78. var hour = (0, _index3.default)(originalDate.getHours(), 2);
  79. var minute = (0, _index3.default)(originalDate.getMinutes(), 2);
  80. var second = (0, _index3.default)(originalDate.getSeconds(), 2);
  81. // If there's also date, separate it with time with a space
  82. var separator = result === '' ? '' : ' ';
  83. // HHmmss or HH:mm:ss.
  84. result = "".concat(result).concat(separator).concat(hour).concat(timeDelimiter).concat(minute).concat(timeDelimiter).concat(second);
  85. }
  86. return result;
  87. }
  88. module.exports = exports.default;