index.js 847 B

12345678910111213141516171819202122232425262728
  1. var parse = require('../parse/index.js')
  2. var setMonth = require('../set_month/index.js')
  3. /**
  4. * @category Quarter Helpers
  5. * @summary Set the year quarter to the given date.
  6. *
  7. * @description
  8. * Set the year quarter to the given date.
  9. *
  10. * @param {Date|String|Number} date - the date to be changed
  11. * @param {Number} quarter - the quarter of the new date
  12. * @returns {Date} the new date with the quarter setted
  13. *
  14. * @example
  15. * // Set the 2nd quarter to 2 July 2014:
  16. * var result = setQuarter(new Date(2014, 6, 2), 2)
  17. * //=> Wed Apr 02 2014 00:00:00
  18. */
  19. function setQuarter (dirtyDate, dirtyQuarter) {
  20. var date = parse(dirtyDate)
  21. var quarter = Number(dirtyQuarter)
  22. var oldQuarter = Math.floor(date.getMonth() / 3) + 1
  23. var diff = quarter - oldQuarter
  24. return setMonth(date, date.getMonth() + diff * 3)
  25. }
  26. module.exports = setQuarter