index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = setYear;
  6. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * @name setYear
  12. * @category Year Helpers
  13. * @summary Set the year to the given date.
  14. *
  15. * @description
  16. * Set the year to the given date.
  17. *
  18. * ### v2.0.0 breaking changes:
  19. *
  20. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  21. *
  22. * @param {Date|Number} date - the date to be changed
  23. * @param {Number} year - the year of the new date
  24. * @returns {Date} the new date with the year set
  25. * @throws {TypeError} 2 arguments required
  26. *
  27. * @example
  28. * // Set year 2013 to 1 September 2014:
  29. * const result = setYear(new Date(2014, 8, 1), 2013)
  30. * //=> Sun Sep 01 2013 00:00:00
  31. */
  32. function setYear(dirtyDate, dirtyYear) {
  33. (0, _index3.default)(2, arguments);
  34. var date = (0, _index2.default)(dirtyDate);
  35. var year = (0, _index.default)(dirtyYear); // Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
  36. if (isNaN(date.getTime())) {
  37. return new Date(NaN);
  38. }
  39. date.setFullYear(year);
  40. return date;
  41. }
  42. module.exports = exports.default;