import React from 'react';
import l from './lang';
import moment from 'moment';

const options = {
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  hour12: false
};

function dateStr(date: Date) {
  return new Intl.DateTimeFormat('de-DE', options).format(date);
}

type DateTimeProps = {
  datetime: Date | string;
  [x: string]: any;
};

export const Datetime: React.FC<DateTimeProps> = ({ datetime, ...props }) => {
  const mDateTime = moment(datetime);
  const date = mDateTime.isValid() ? mDateTime.toDate() : moment().toDate();
  return <span {...props}>{dateStr(date)}</span>;
};
