index.js 4.0 KB

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