startOfYear.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link startOfYear} function options.
  4. */
  5. /**
  6. * @name startOfYear
  7. * @category Year Helpers
  8. * @summary Return the start of a year for the given date.
  9. *
  10. * @description
  11. * Return the start of a year for the given date.
  12. * The result will be in the local timezone.
  13. *
  14. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  15. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  16. *
  17. * @param date - The original date
  18. * @param options - The options
  19. *
  20. * @returns The start of a year
  21. *
  22. * @example
  23. * // The start of a year for 2 September 2014 11:55:00:
  24. * const result = startOfYear(new Date(2014, 8, 2, 11, 55, 00))
  25. * //=> Wed Jan 01 2014 00:00:00
  26. */
  27. export function startOfYear(date, options) {
  28. const date_ = toDate(date, options?.in);
  29. date_.setFullYear(date_.getFullYear(), 0, 1);
  30. date_.setHours(0, 0, 0, 0);
  31. return date_;
  32. }
  33. // Fallback for modularized imports:
  34. export default startOfYear;