index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = startOfDecade;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name startOfDecade
  11. * @category Decade Helpers
  12. * @summary Return the start of a decade for the given date.
  13. *
  14. * @description
  15. * Return the start of a decade for the given date.
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * @param {Date|Number} date - the original date
  22. * @returns {Date} the start of a decade
  23. * @throws {TypeError} 1 argument required
  24. *
  25. * @example
  26. * // The start of a decade for 21 October 2015 00:00:00:
  27. * const result = startOfDecade(new Date(2015, 9, 21, 00, 00, 00))
  28. * //=> Jan 01 2010 00:00:00
  29. */
  30. function startOfDecade(dirtyDate) {
  31. (0, _index2.default)(1, arguments);
  32. var date = (0, _index.default)(dirtyDate);
  33. var year = date.getFullYear();
  34. var decade = Math.floor(year / 10) * 10;
  35. date.setFullYear(decade, 0, 1);
  36. date.setHours(0, 0, 0, 0);
  37. return date;
  38. }
  39. module.exports = exports.default;