index.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name startOfDecade
  5. * @category Decade Helpers
  6. * @summary Return the start of a decade for the given date.
  7. *
  8. * @description
  9. * Return the start of a decade for 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 original date
  16. * @returns {Date} the start of a decade
  17. * @throws {TypeError} 1 argument required
  18. *
  19. * @example
  20. * // The start of a decade for 21 October 2015 00:00:00:
  21. * const result = startOfDecade(new Date(2015, 9, 21, 00, 00, 00))
  22. * //=> Jan 01 2010 00:00:00
  23. */
  24. export default function startOfDecade(dirtyDate) {
  25. requiredArgs(1, arguments);
  26. var date = toDate(dirtyDate);
  27. var year = date.getFullYear();
  28. var decade = Math.floor(year / 10) * 10;
  29. date.setFullYear(decade, 0, 1);
  30. date.setHours(0, 0, 0, 0);
  31. return date;
  32. }