function fmtYmd(d: Date): string {
  const y = d.getUTCFullYear();
  const m = String(d.getUTCMonth() + 1).padStart(2, '0');
  const day = String(d.getUTCDate()).padStart(2, '0');
  return `${y}-${m}-${day}`;
}

export function todayYmdUTC(): string {
  return fmtYmd(new Date());
}

export function compareYmd(a: string, b: string): number {
  // returns -1 if a<b, 0 if equal, 1 if a>b
  if (a === b) return 0;
  return a < b ? -1 : 1;
}

function addDaysYmd(ymd: string, days: number): string {
  const d = new Date(`${ymd}T00:00:00Z`);
  d.setUTCDate(d.getUTCDate() + days);
  return fmtYmd(d);
}

export function ymdToDmy(ymd: string): string {
  const m = /^\s*(\d{4})-(\d{2})-(\d{2})\s*$/.exec(ymd || '');
  if (!m) return ymd;
  return `${m[3]}/${m[2]}/${m[1]}`;
}

export function isToday(ymd: string): boolean {
  return ymd === todayYmdUTC();
}

export function isTomorrow(ymd: string): boolean {
  return ymd === addDaysYmd(todayYmdUTC(), 1);
}
