All files / src/components date.ts

100% Statements 17/17
100% Branches 11/11
100% Functions 2/2
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 339x                 9x 5x 3x     2x 2x     9x 3x 1x   1x 1x 1x 1x 1x 1x     1x    
export const DATE_FORMAT: Intl.DateTimeFormatOptions = {
  year: "numeric",
  month: "2-digit",
  day: "2-digit",
  hour: "2-digit",
  minute: "2-digit",
  second: "2-digit",
};
 
export const dateParser = (v: string | number | Date | null | undefined): number | null => {
  if (v === "" || v === null || v === undefined) {
    return null;
  }
 
  const d = new Date(v);
  return Number.isNaN(d.getTime()) ? null : d.getTime();
};
 
export const dateFormatter = (v: string | number | Date | undefined | null): string => {
  if (v === undefined || v === null) return "";
  const d = new Date(v);
 
  const pad = "00";
  const year = d.getFullYear().toString();
  const month = (pad + (d.getMonth() + 1).toString()).slice(-2);
  const day = (pad + d.getDate().toString()).slice(-2);
  const hour = (pad + d.getHours().toString()).slice(-2);
  const minute = (pad + d.getMinutes().toString()).slice(-2);
 
  // target format yyyy-MM-ddThh:mm
  return `${year}-${month}-${day}T${hour}:${minute}`;
};