export const now = () => new Date();
export const isValidDate = (value) => new Date(value).toString() !== 'Invalid Date';
export const year = (date) => date.getFullYear();
export const month = (date) => date.getMonth();
export const day = (date) => date.getDate();
export const increaseYear = (date, years) => year(date) + years;
export const decreaseYear = (date, years) => year(date) - years;
export const isInRange = (date, minDate, maxDate) => {
  if (date && minDate && maxDate) {
    return minDate <= date && date <= maxDate;
  }
  return true;
};
export const format = (date) => {
  function pad(value) {
    var s = `0${value}`;
    return s.substr(s.length - 2);
  }
  if (!isValidDate(date)) {
    return '';
  }
  return `${pad(day(date))}.${pad(month(date) + 1)}.${year(date)}`;
};
export const getLastDayOfMonth = (year, month) => {
  const date = new Date(year, month + 1, 0);
  if (!isValidDate(date)) {
    return 0;
  }
  return date.getDate();
};
export const getFirstDayOfTheWeek = (date) => {
  if (!isValidDate(date)) {
    return date;
  }
  const weekdayDiff = [6, 0, 1, 2, 3, 4, 5];
  const d = new Date(date);
  d.setDate(d.getDate() - weekdayDiff[d.getDay()]);
  return d;
};
export const isSameYear = (a, b) => {
  if (!isValidDate(a) || !isValidDate(b)) {
    return false;
  }
  return a.getFullYear() === b.getFullYear();
};
export const isSameMonth = (a, b) => {
  if (!isValidDate(a) || !isValidDate(b)) {
    return false;
  }
  return isSameYear(a, b) && a.getMonth() === b.getMonth();
};
export const isSameDay = (a, b) => {
  if (!isValidDate(a) || !isValidDate(b)) {
    return false;
  }
  return isSameMonth(a, b) && a.getDate() === b.getDate();
};
export const isSameWeek = (a, b) => {
  if (!isValidDate(a) || !isValidDate(b)) {
    return false;
  }
  return isSameDay(getFirstDayOfTheWeek(a), getFirstDayOfTheWeek(b));
};
