formatRFC3339.js 2.8 KB

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