'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var isUdf = _interopDefault(require('./isUdf'));
var formatToDate = _interopDefault(require('./formatToDate'));
var getMonthDays = _interopDefault(require('./getMonthDays'));

/**
 * 获取给定时间的日期在当年的第几天
 * @param {Date|number|string} time
 * @returns {number}
 *
 * @example
 * getDayOfYear();             // => 返回当前日期在一年中的天数
 * getDayOfYear('2021-10-01'); // => 274
 */
function getDayOfYear(time) {
  if (isUdf(time)) time = new Date();
  var date = formatToDate(time);
  if (!date) return NaN;
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var days = date.getDate();

  for (var i = 1; i < month; i++) {
    days += getMonthDays("".concat(year, "-").concat(i));
  }

  return days;
}

module.exports = getDayOfYear;
