index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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, dirtyOptions) {
  37. if (arguments.length < 1) {
  38. throw new TypeError("1 arguments required, but only ".concat(arguments.length, " present"));
  39. }
  40. var originalDate = toDate(dirtyDate);
  41. if (!isValid(originalDate)) {
  42. throw new RangeError('Invalid time value');
  43. }
  44. var options = dirtyOptions || {};
  45. var fractionDigits = options.fractionDigits == null ? 0 : toInteger(options.fractionDigits); // Test if fractionDigits is between 0 and 3 _and_ is not NaN
  46. if (!(fractionDigits >= 0 && fractionDigits <= 3)) {
  47. throw new RangeError('fractionDigits must be between 0 and 3 inclusively');
  48. }
  49. var day = addLeadingZeros(originalDate.getDate(), 2);
  50. var month = addLeadingZeros(originalDate.getMonth() + 1, 2);
  51. var year = originalDate.getFullYear();
  52. var hour = addLeadingZeros(originalDate.getHours(), 2);
  53. var minute = addLeadingZeros(originalDate.getMinutes(), 2);
  54. var second = addLeadingZeros(originalDate.getSeconds(), 2);
  55. var fractionalSecond = '';
  56. if (fractionDigits > 0) {
  57. var milliseconds = originalDate.getMilliseconds();
  58. var fractionalSeconds = Math.floor(milliseconds * Math.pow(10, fractionDigits - 3));
  59. fractionalSecond = '.' + addLeadingZeros(fractionalSeconds, fractionDigits);
  60. }
  61. var offset = '';
  62. var tzOffset = originalDate.getTimezoneOffset();
  63. if (tzOffset !== 0) {
  64. var absoluteOffset = Math.abs(tzOffset);
  65. var hourOffset = addLeadingZeros(toInteger(absoluteOffset / 60), 2);
  66. var minuteOffset = addLeadingZeros(absoluteOffset % 60, 2); // If less than 0, the sign is +, because it is ahead of time.
  67. var sign = tzOffset < 0 ? '+' : '-';
  68. offset = "".concat(sign).concat(hourOffset, ":").concat(minuteOffset);
  69. } else {
  70. offset = 'Z';
  71. }
  72. return "".concat(year, "-").concat(month, "-").concat(day, "T").concat(hour, ":").concat(minute, ":").concat(second).concat(fractionalSecond).concat(offset);
  73. }