index.js 814 B

123456789101112131415161718192021222324252627282930
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name getYear
  5. * @category Year Helpers
  6. * @summary Get the year of the given date.
  7. *
  8. * @description
  9. * Get the year of the given date.
  10. *
  11. * ### v2.0.0 breaking changes:
  12. *
  13. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  14. *
  15. * @param {Date|Number} date - the given date
  16. * @returns {Number} the year
  17. * @throws {TypeError} 1 argument required
  18. *
  19. * @example
  20. * // Which year is 2 July 2014?
  21. * const result = getYear(new Date(2014, 6, 2))
  22. * //=> 2014
  23. */
  24. export default function getYear(dirtyDate) {
  25. requiredArgs(1, arguments);
  26. var date = toDate(dirtyDate);
  27. var year = date.getFullYear();
  28. return year;
  29. }