index.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import toDate from "../toDate/index.js";
  2. import isValid from "../isValid/index.js";
  3. import addLeadingZeros from "../_lib/addLeadingZeros/index.js";
  4. import toInteger from "../_lib/toInteger/index.js";
  5. /**
  6. * @name formatRFC3339
  7. * @category Common Helpers
  8. * @summary Format the date according to the RFC 3339 standard (https://tools.ietf.org/html/rfc3339#section-5.6).
  9. *
  10. * @description
  11. * Return the formatted date string in RFC 3339 format. Options may be passed to control the parts and notations of the date.
  12. *
  13. * @param {Date|Number} date - the original date
  14. * @param {Object} [options] - an object with options.
  15. * @param {0|1|2|3} [options.fractionDigits=0] - number of digits after the decimal point after seconds
  16. * @returns {String} the formatted date string
  17. * @throws {TypeError} 1 argument required
  18. * @throws {RangeError} `date` must not be Invalid Date
  19. * @throws {RangeError} `options.fractionDigits` must be between 0 and 3
  20. *
  21. * @example
  22. * // Represent 18 September 2019 in RFC 3339 format:
  23. * const result = formatRFC3339(new Date(2019, 8, 18, 19, 0, 52))
  24. * //=> '2019-09-18T19:00:52Z'
  25. *
  26. * @example
  27. * // Represent 18 September 2019 in RFC 3339 format, 2 digits of second fraction:
  28. * const result = formatRFC3339(new Date(2019, 8, 18, 19, 0, 52, 234), { fractionDigits: 2 })
  29. * //=> '2019-09-18T19:00:52.23Z'
  30. *
  31. * @example
  32. * // Represent 18 September 2019 in RFC 3339 format, 3 digits of second fraction
  33. * const result = formatRFC3339(new Date(2019, 8, 18, 19, 0, 52, 234), { fractionDigits: 3 })
  34. * //=> '2019-09-18T19:00:52.234Z'
  35. */
  36. export default function formatRFC3339(dirtyDate, options) {
  37. var _options$fractionDigi;
  38. if (arguments.length < 1) {
  39. throw new TypeError("1 arguments required, but only ".concat(arguments.length, " present"));
  40. }
  41. var originalDate = toDate(dirtyDate);
  42. if (!isValid(originalDate)) {
  43. throw new RangeError('Invalid time value');
  44. }
  45. var fractionDigits = Number((_options$fractionDigi = options === null || options === void 0 ? void 0 : options.fractionDigits) !== null && _options$fractionDigi !== void 0 ? _options$fractionDigi : 0);
  46. // Test if fractionDigits is between 0 and 3 _and_ is not NaN
  47. if (!(fractionDigits >= 0 && fractionDigits <= 3)) {
  48. throw new RangeError('fractionDigits must be between 0 and 3 inclusively');
  49. }
  50. var day = addLeadingZeros(originalDate.getDate(), 2);
  51. var month = addLeadingZeros(originalDate.getMonth() + 1, 2);
  52. var year = originalDate.getFullYear();
  53. var hour = addLeadingZeros(originalDate.getHours(), 2);
  54. var minute = addLeadingZeros(originalDate.getMinutes(), 2);
  55. var second = addLeadingZeros(originalDate.getSeconds(), 2);
  56. var fractionalSecond = '';
  57. if (fractionDigits > 0) {
  58. var milliseconds = originalDate.getMilliseconds();
  59. var fractionalSeconds = Math.floor(milliseconds * Math.pow(10, fractionDigits - 3));
  60. fractionalSecond = '.' + addLeadingZeros(fractionalSeconds, fractionDigits);
  61. }
  62. var offset = '';
  63. var tzOffset = originalDate.getTimezoneOffset();
  64. if (tzOffset !== 0) {
  65. var absoluteOffset = Math.abs(tzOffset);
  66. var hourOffset = addLeadingZeros(toInteger(absoluteOffset / 60), 2);
  67. var minuteOffset = addLeadingZeros(absoluteOffset % 60, 2);
  68. // If less than 0, the sign is +, because it is ahead of time.
  69. var sign = tzOffset < 0 ? '+' : '-';
  70. offset = "".concat(sign).concat(hourOffset, ":").concat(minuteOffset);
  71. } else {
  72. offset = 'Z';
  73. }
  74. return "".concat(year, "-").concat(month, "-").concat(day, "T").concat(hour, ":").concat(minute, ":").concat(second).concat(fractionalSecond).concat(offset);
  75. }