index.js 1.0 KB

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