formatISO9075.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. exports.formatISO9075 = formatISO9075;
  3. var _index = require("./isValid.js");
  4. var _index2 = require("./toDate.js");
  5. var _index3 = require("./_lib/addLeadingZeros.js");
  6. /**
  7. * The {@link formatISO9075} function options.
  8. */
  9. /**
  10. * @name formatISO9075
  11. * @category Common Helpers
  12. * @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).
  13. *
  14. * @description
  15. * Return the formatted date string in ISO 9075 format. Options may be passed to control the parts and notations of the date.
  16. *
  17. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  18. *
  19. * @param date - The original date
  20. * @param options - An object with options.
  21. *
  22. * @returns The formatted date string
  23. *
  24. * @throws `date` must not be Invalid Date
  25. *
  26. * @example
  27. * // Represent 18 September 2019 in ISO 9075 format:
  28. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52))
  29. * //=> '2019-09-18 19:00:52'
  30. *
  31. * @example
  32. * // Represent 18 September 2019 in ISO 9075, short format:
  33. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
  34. * //=> '20190918 190052'
  35. *
  36. * @example
  37. * // Represent 18 September 2019 in ISO 9075 format, date only:
  38. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
  39. * //=> '2019-09-18'
  40. *
  41. * @example
  42. * // Represent 18 September 2019 in ISO 9075 format, time only:
  43. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  44. * //=> '19:00:52'
  45. */
  46. function formatISO9075(date, options) {
  47. const _date = (0, _index2.toDate)(date);
  48. if (!(0, _index.isValid)(_date)) {
  49. throw new RangeError("Invalid time value");
  50. }
  51. const format = options?.format ?? "extended";
  52. const representation = options?.representation ?? "complete";
  53. let result = "";
  54. const dateDelimiter = format === "extended" ? "-" : "";
  55. const timeDelimiter = format === "extended" ? ":" : "";
  56. // Representation is either 'date' or 'complete'
  57. if (representation !== "time") {
  58. const day = (0, _index3.addLeadingZeros)(_date.getDate(), 2);
  59. const month = (0, _index3.addLeadingZeros)(_date.getMonth() + 1, 2);
  60. const year = (0, _index3.addLeadingZeros)(_date.getFullYear(), 4);
  61. // yyyyMMdd or yyyy-MM-dd.
  62. result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
  63. }
  64. // Representation is either 'time' or 'complete'
  65. if (representation !== "date") {
  66. const hour = (0, _index3.addLeadingZeros)(_date.getHours(), 2);
  67. const minute = (0, _index3.addLeadingZeros)(_date.getMinutes(), 2);
  68. const second = (0, _index3.addLeadingZeros)(_date.getSeconds(), 2);
  69. // If there's also date, separate it with time with a space
  70. const separator = result === "" ? "" : " ";
  71. // HHmmss or HH:mm:ss.
  72. result = `${result}${separator}${hour}${timeDelimiter}${minute}${timeDelimiter}${second}`;
  73. }
  74. return result;
  75. }