All files / src/helpers dates.ts

38.33% Statements 23/60
12.9% Branches 4/31
27.27% Functions 3/11
43.14% Lines 22/51

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                    5x 5x 5x 5x       5x                     5x               38x     38x 38x         5x   5x   5x 8x     5x 11x 11x 11x     5x           5x               5x                                                 5x           5x        
import {
  format as _format,
  differenceInDays,
  differenceInMinutes,
  differenceInCalendarDays
} from 'date-fns';
import de from 'date-fns/locale/de';
import nl from 'date-fns/locale/nl';
import { toDate, utcToZonedTime } from 'date-fns-tz';
 
const locales: any = { de, nl };
export const timeZone = 'Europe/Berlin';
export const now = utcToZonedTime(new Date(), timeZone);
export const formattedNow = _format(now, 'yyyy-MM-dd');
 
export { differenceInDays, differenceInCalendarDays, utcToZonedTime, toDate };
 
export const difference = function(
  dateLeft: string | Date,
  dateRight: string | Date
) {
  if (typeof dateLeft === 'string') dateLeft = new Date(dateLeft);
  if (typeof dateRight === 'string') dateRight = new Date(dateRight);
  const diffMin = differenceInMinutes(dateLeft, dateRight);
  const min = diffMin % 60;
  return `${Math.round(diffMin / 60)}:${min < 10 ? '0' + min : min}`;
};
 
export const format = function(
  date: string | Date,
  formatStr: string,
  currentLocale?: string
) {
  // const locale = !!currentLocale
  //   ? currentLocale
  //   : localStorage.getItem('i18nextLng');
  const locale = !!currentLocale
    ? currentLocale
    : 'de';
  if (typeof date === 'string') date = toDate(date);
  return _format(date, formatStr, {
    locale: locale ? locales[locale] : locales['de']
  });
};
 
export const formatISO: (date: string | Date) => string = (date) => format(date, 'yyyy-MM-dd');
 
export const formatToDate: (date: string | Date) => string = (date) => format(date, 'dd.MM.yyyy');
 
export const isLater = (date1: string | Date, date2: string | Date) => {
  return new Date(date1).getTime() > new Date(date2).getTime();
};
 
export const getNextDay = (_date: string | Date) => {
  const date = new Date(_date);
  date.setDate(date.getDate() + 1);
  return date;
};
 
export const getDateInXDay = (_date: string | Date, count: number) => {
  const date = new Date(_date);
  date.setDate(date.getDate() + count);
  return date;
};
 
export const isSameDay = (d1: Date, d2: Date) => {
  return (
    d1.getFullYear() === d2.getFullYear() &&
    d1.getMonth() === d2.getMonth() &&
    d1.getDate() === d2.getDate()
  );
};
 
export const getNextAvailableDate = (offWeekDays: number[]) => {
  const today = new Date();
  let todayWeekDay = today.getDay();
  if (todayWeekDay === 0) todayWeekDay = 7;
 
  let diffToNextAvailableDay;
 
  for (let i = todayWeekDay; i < 8; i++) {
    if (!offWeekDays.includes(i)) {
      diffToNextAvailableDay = i - todayWeekDay;
      break;
    }
  }
  if (!diffToNextAvailableDay) {
    for (let i = 1; i < todayWeekDay; i++) {
      if (!offWeekDays.includes(i)) {
        diffToNextAvailableDay = i + todayWeekDay;
        break;
      }
    }
  }
  if (!diffToNextAvailableDay) return null;
  else return getDateInXDay(today, diffToNextAvailableDay);
};
 
export const getMonthDays: (month: number, year: number) => number = (month, year) => {
  const months30 = [4, 6, 9, 11];
  const leapYear = year % 4 === 0;
  return month === 2 ? (leapYear ? 29 : 28) : months30.includes(month) ? 30 : 31;
};
 
export const utcString: (dateStr: string, hours: number) => string = (dateStr, hours) => {
  const date = new Date(dateStr);
  date.setUTCHours(hours);
  return date.toISOString();
};