import { DateFormatOptions } from '@msfa-models/date-format-options.model';

export function formatToDate(date: string): Date {
  date = date.replace(/-/g, '');
  const year = date.substring(0, 4);
  const month = date.substring(4, 6) || '01';
  const day = date.substring(6, 8) || '01';

  return new Date(`${year}-${month}-${day}`);
}

export function formatDate(date: Date, locale: string = 'sv-SE', options?: DateFormatOptions): string {
  const formatOptions: DateFormatOptions = options || {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
  };

  return new Date(date).toLocaleDateString(locale, formatOptions);
}
