export interface IDate {
  year: string;
  month: string;
  day: string;
}

export const getDateAsString = (date: Date | IDate | string): string => {
  if (date instanceof Date) {
    return `${date?.getFullYear()}-${date?.getMonth() + 1}-${date?.getDate()}`;
  }
  if (typeof date === "string") {
    return date;
  }
  return `${date?.year}-${date?.month}-${date?.day}`;
};

export const getAge = (date: Date | string) => {
  const today = new Date();
  const birthDate = new Date(date);
  let age = today?.getFullYear() - birthDate?.getFullYear();
  const m = today?.getMonth() - birthDate?.getMonth();
  if (m < 0 || (m === 0 && today?.getDate() < birthDate?.getDate())) {
    age--;
  }
  return age;
};

export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));