formatISO9075.cjs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. exports.formatISO9075 = formatISO9075;
  3. var _index = require("./_lib/addLeadingZeros.cjs");
  4. var _index2 = require("./isValid.cjs");
  5. var _index3 = require("./toDate.cjs");
  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. * @param date - The original date
  18. * @param options - An object with options.
  19. *
  20. * @returns The formatted date string
  21. *
  22. * @throws `date` must not be Invalid Date
  23. *
  24. * @example
  25. * // Represent 18 September 2019 in ISO 9075 format:
  26. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52))
  27. * //=> '2019-09-18 19:00:52'
  28. *
  29. * @example
  30. * // Represent 18 September 2019 in ISO 9075, short format:
  31. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { format: 'basic' })
  32. * //=> '20190918 190052'
  33. *
  34. * @example
  35. * // Represent 18 September 2019 in ISO 9075 format, date only:
  36. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'date' })
  37. * //=> '2019-09-18'
  38. *
  39. * @example
  40. * // Represent 18 September 2019 in ISO 9075 format, time only:
  41. * const result = formatISO9075(new Date(2019, 8, 18, 19, 0, 52), { representation: 'time' })
  42. * //=> '19:00:52'
  43. */
  44. function formatISO9075(date, options) {
  45. const date_ = (0, _index3.toDate)(date, options?.in);
  46. if (!(0, _index2.isValid)(date_)) {
  47. throw new RangeError("Invalid time value");
  48. }
  49. const format = options?.format ?? "extended";
  50. const representation = options?.representation ?? "complete";
  51. let result = "";
  52. const dateDelimiter = format === "extended" ? "-" : "";
  53. const timeDelimiter = format === "extended" ? ":" : "";
  54. // Representation is either 'date' or 'complete'
  55. if (representation !== "time") {
  56. const day = (0, _index.addLeadingZeros)(date_.getDate(), 2);
  57. const month = (0, _index.addLeadingZeros)(date_.getMonth() + 1, 2);
  58. const year = (0, _index.addLeadingZeros)(date_.getFullYear(), 4);
  59. // yyyyMMdd or yyyy-MM-dd.
  60. result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
  61. }
  62. // Representation is either 'time' or 'complete'
  63. if (representation !== "date") {
  64. const hour = (0, _index.addLeadingZeros)(date_.getHours(), 2);
  65. const minute = (0, _index.addLeadingZeros)(date_.getMinutes(), 2);
  66. const second = (0, _index.addLeadingZeros)(date_.getSeconds(), 2);
  67. // If there's also date, separate it with time with a space
  68. const separator = result === "" ? "" : " ";
  69. // HHmmss or HH:mm:ss.
  70. result = `${result}${separator}${hour}${timeDelimiter}${minute}${timeDelimiter}${second}`;
  71. }
  72. return result;
  73. }