getDayOfYear.js 555 B

12345678910111213141516171819202122
  1. var getWhatYear = require('./getWhatYear')
  2. var toStringDate = require('./toStringDate')
  3. var isValidDate = require('./isValidDate')
  4. var isLeapYear = require('./isLeapYear')
  5. /**
  6. * 返回某个年份的天数
  7. *
  8. * @param {Date} date 日期或数字
  9. * @param {Number} offset 年(默认当年)、前几个年、后几个年
  10. * @return {Number}
  11. */
  12. function getDayOfYear (date, year) {
  13. date = toStringDate(date)
  14. if (isValidDate(date)) {
  15. return isLeapYear(getWhatYear(date, year)) ? 366 : 365
  16. }
  17. return NaN
  18. }
  19. module.exports = getDayOfYear