formatRFC3339.mjs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { isValid } from "./isValid.mjs";
  2. import { toDate } from "./toDate.mjs";
  3. import { addLeadingZeros } from "./_lib/addLeadingZeros.mjs";
  4. /**
  5. * The {@link formatRFC3339} function options.
  6. */
  7. /**
  8. * @name formatRFC3339
  9. * @category Common Helpers
  10. * @summary Format the date according to the RFC 3339 standard (https://tools.ietf.org/html/rfc3339#section-5.6).
  11. *
  12. * @description
  13. * Return the formatted date string in RFC 3339 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 RFC 3339 format:
  26. * formatRFC3339(new Date(2019, 8, 18, 19, 0, 52))
  27. * //=> '2019-09-18T19:00:52Z'
  28. *
  29. * @example
  30. * // Represent 18 September 2019 in RFC 3339 format, 3 digits of second fraction
  31. * formatRFC3339(new Date(2019, 8, 18, 19, 0, 52, 234), {
  32. * fractionDigits: 3
  33. * })
  34. * //=> '2019-09-18T19:00:52.234Z'
  35. */
  36. export function formatRFC3339(date, options) {
  37. const _date = toDate(date);
  38. if (!isValid(_date)) {
  39. throw new RangeError("Invalid time value");
  40. }
  41. const fractionDigits = options?.fractionDigits ?? 0;
  42. const day = addLeadingZeros(_date.getDate(), 2);
  43. const month = addLeadingZeros(_date.getMonth() + 1, 2);
  44. const year = _date.getFullYear();
  45. const hour = addLeadingZeros(_date.getHours(), 2);
  46. const minute = addLeadingZeros(_date.getMinutes(), 2);
  47. const second = addLeadingZeros(_date.getSeconds(), 2);
  48. let fractionalSecond = "";
  49. if (fractionDigits > 0) {
  50. const milliseconds = _date.getMilliseconds();
  51. const fractionalSeconds = Math.trunc(
  52. milliseconds * Math.pow(10, fractionDigits - 3),
  53. );
  54. fractionalSecond = "." + addLeadingZeros(fractionalSeconds, fractionDigits);
  55. }
  56. let offset = "";
  57. const tzOffset = _date.getTimezoneOffset();
  58. if (tzOffset !== 0) {
  59. const absoluteOffset = Math.abs(tzOffset);
  60. const hourOffset = addLeadingZeros(Math.trunc(absoluteOffset / 60), 2);
  61. const minuteOffset = addLeadingZeros(absoluteOffset % 60, 2);
  62. // If less than 0, the sign is +, because it is ahead of time.
  63. const sign = tzOffset < 0 ? "+" : "-";
  64. offset = `${sign}${hourOffset}:${minuteOffset}`;
  65. } else {
  66. offset = "Z";
  67. }
  68. return `${year}-${month}-${day}T${hour}:${minute}:${second}${fractionalSecond}${offset}`;
  69. }
  70. // Fallback for modularized imports:
  71. export default formatRFC3339;