formatISO9075.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { addLeadingZeros } from "./_lib/addLeadingZeros.js";
  2. import { isValid } from "./isValid.js";
  3. import { toDate } from "./toDate.js";
  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. * @param date - The original date
  16. * @param options - An object with options.
  17. *
  18. * @returns The formatted date string
  19. *
  20. * @throws `date` must not be Invalid Date
  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 function formatISO9075(date, options) {
  43. const date_ = toDate(date, options?.in);
  44. if (!isValid(date_)) {
  45. throw new RangeError("Invalid time value");
  46. }
  47. const format = options?.format ?? "extended";
  48. const representation = options?.representation ?? "complete";
  49. let result = "";
  50. const dateDelimiter = format === "extended" ? "-" : "";
  51. const timeDelimiter = format === "extended" ? ":" : "";
  52. // Representation is either 'date' or 'complete'
  53. if (representation !== "time") {
  54. const day = addLeadingZeros(date_.getDate(), 2);
  55. const month = addLeadingZeros(date_.getMonth() + 1, 2);
  56. const year = addLeadingZeros(date_.getFullYear(), 4);
  57. // yyyyMMdd or yyyy-MM-dd.
  58. result = `${year}${dateDelimiter}${month}${dateDelimiter}${day}`;
  59. }
  60. // Representation is either 'time' or 'complete'
  61. if (representation !== "date") {
  62. const hour = addLeadingZeros(date_.getHours(), 2);
  63. const minute = addLeadingZeros(date_.getMinutes(), 2);
  64. const second = addLeadingZeros(date_.getSeconds(), 2);
  65. // If there's also date, separate it with time with a space
  66. const separator = result === "" ? "" : " ";
  67. // HHmmss or HH:mm:ss.
  68. result = `${result}${separator}${hour}${timeDelimiter}${minute}${timeDelimiter}${second}`;
  69. }
  70. return result;
  71. }
  72. // Fallback for modularized imports:
  73. export default formatISO9075;