import isUdf from './isUdf';
import getDayStartTime from './getDayStartTime';
import { DAY_TIME } from './consts';

/**
 * 获取两个时间之前相差的天数
 * @param {Date|number|string} time1 时间1
 * @param {Date|number|string} time2 时间2，默认当天
 * @returns
 *
 * @example
 * getDaysBetween('2021-01-01', '2021-10-01'); // => 273
 */
function getDaysBetween(time1, time2) {
  if (isUdf(time1)) return NaN;
  var timeStamp1 = getDayStartTime(time1);
  if (Number.isNaN(timeStamp1)) return NaN;
  var timeStamp2 = getDayStartTime(time2);
  if (Number.isNaN(timeStamp2)) return NaN;
  return (timeStamp2 - timeStamp1) / DAY_TIME;
}

export default getDaysBetween;
