index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = formatISO9075;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../isValid/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/addLeadingZeros/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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.represenation` 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, dirtyOptions) {
  49. if (arguments.length < 1) {
  50. throw new TypeError("1 argument required, but only ".concat(arguments.length, " present"));
  51. }
  52. var originalDate = (0, _index.default)(dirtyDate);
  53. if (!(0, _index2.default)(originalDate)) {
  54. throw new RangeError('Invalid time value');
  55. }
  56. var options = dirtyOptions || {};
  57. var format = options.format == null ? 'extended' : String(options.format);
  58. var representation = options.representation == null ? 'complete' : String(options.representation);
  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' ? ':' : ''; // Representation is either 'date' or 'complete'
  68. if (representation !== 'time') {
  69. var day = (0, _index3.default)(originalDate.getDate(), 2);
  70. var month = (0, _index3.default)(originalDate.getMonth() + 1, 2);
  71. var year = (0, _index3.default)(originalDate.getFullYear(), 4); // yyyyMMdd or yyyy-MM-dd.
  72. result = "".concat(year).concat(dateDelimiter).concat(month).concat(dateDelimiter).concat(day);
  73. } // Representation is either 'time' or 'complete'
  74. if (representation !== 'date') {
  75. var hour = (0, _index3.default)(originalDate.getHours(), 2);
  76. var minute = (0, _index3.default)(originalDate.getMinutes(), 2);
  77. var second = (0, _index3.default)(originalDate.getSeconds(), 2); // If there's also date, separate it with time with a space
  78. var separator = result === '' ? '' : ' '; // HHmmss or HH:mm:ss.
  79. result = "".concat(result).concat(separator).concat(hour).concat(timeDelimiter).concat(minute).concat(timeDelimiter).concat(second);
  80. }
  81. return result;
  82. }
  83. module.exports = exports.default;