formatISO9075.mjs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { isValid } from "./isValid.mjs";
  2. import { toDate } from "./toDate.mjs";
  3. import { addLeadingZeros } from "./_lib/addLeadingZeros.mjs";
  4. /**
  5. * The {@link formatISO9075} function options.
  6. */
  7. /**
  8. * @name formatISO9075
  9. * @category Common Helpers
  10. * @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).
  11. *
  12. * @description
  13. * Return the formatted date string in ISO 9075 format. Options may be passed to control the parts and notations of the date.
  14. *
  15. * @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).
  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. export function formatISO9075(date, options) {
  45. const _date = toDate(date);
  46. if (!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 = addLeadingZeros(_date.getDate(), 2);
  57. const month = addLeadingZeros(_date.getMonth() + 1, 2);
  58. const year = 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 = addLeadingZeros(_date.getHours(), 2);
  65. const minute = addLeadingZeros(_date.getMinutes(), 2);
  66. const second = 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. }
  74. // Fallback for modularized imports:
  75. export default formatISO9075;