index.js 1.1 KB

1234567891011121314151617181920212223242526
  1. import toDate from "../../toDate/index.js";
  2. import startOfUTCISOWeek from "../startOfUTCISOWeek/index.js";
  3. import requiredArgs from "../requiredArgs/index.js"; // This function will be a part of public API when UTC function will be implemented.
  4. // See issue: https://github.com/date-fns/date-fns/issues/376
  5. export default function getUTCISOWeekYear(dirtyDate) {
  6. requiredArgs(1, arguments);
  7. var date = toDate(dirtyDate);
  8. var year = date.getUTCFullYear();
  9. var fourthOfJanuaryOfNextYear = new Date(0);
  10. fourthOfJanuaryOfNextYear.setUTCFullYear(year + 1, 0, 4);
  11. fourthOfJanuaryOfNextYear.setUTCHours(0, 0, 0, 0);
  12. var startOfNextYear = startOfUTCISOWeek(fourthOfJanuaryOfNextYear);
  13. var fourthOfJanuaryOfThisYear = new Date(0);
  14. fourthOfJanuaryOfThisYear.setUTCFullYear(year, 0, 4);
  15. fourthOfJanuaryOfThisYear.setUTCHours(0, 0, 0, 0);
  16. var startOfThisYear = startOfUTCISOWeek(fourthOfJanuaryOfThisYear);
  17. if (date.getTime() >= startOfNextYear.getTime()) {
  18. return year + 1;
  19. } else if (date.getTime() >= startOfThisYear.getTime()) {
  20. return year;
  21. } else {
  22. return year - 1;
  23. }
  24. }