All files / shared/utils timeFormatter.ts

94.28% Statements 33/35
50% Branches 2/4
100% Functions 10/10
94.28% Lines 33/35

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              579x 3106x 3106x     579x 20960x 20960x     579x 547x 547x     579x 801x 801x     579x 20969x 20969x     579x   579x         579x   579x 43x 43x     579x       126x 126x     579x 1195x 1195x   1195x 1195x   1195x     579x 6x 6x        
import dayjs from "dayjs";
import { TIMEZONE } from "shared/utils/initializeDayjs";
 
// dayjs.format() is only called here to make sure all client times use correct timezone
 
/* eslint-disable no-restricted-syntax -- We want to call format() here */
 
export const getWeekdayAndTime = (time: string): string => {
  const timeFormat = "dddd HH:mm";
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const getDate = (time: string): string => {
  const timeFormat = "D.M.YYYY";
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const getShortDate = (time: string): string => {
  const timeFormat = "ddd D.M.";
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const getTime = (time: string): string => {
  const timeFormat = "HH:mm";
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const getShortWeekdayAndTime = (time: string): string => {
  const timeFormat = "ddd HH:mm";
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const timezoneFormat = "z";
 
export const getTimezone = (time: string): string => {
  const timeFormat = timezoneFormat;
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const dateAndTimeFormat = "ddd D.M.YYYY HH:mm";
 
export const getDateAndTime = (time: string): string => {
  const timeFormat = dateAndTimeFormat;
  return dayjs(time).tz(TIMEZONE).format(timeFormat);
};
 
export const getDateAndTimeWithLocale = (
  time: string,
  locale: "fi" | "en",
): string => {
  const timeFormat = dateAndTimeFormat;
  return dayjs(time).tz(TIMEZONE).locale(locale).format(timeFormat);
};
 
export const formatProgramItemDuration = (mins: number): string => {
  const hours = Math.floor(mins / 60);
  const minutes = mins % 60;
 
  const hoursStr = hours === 0 ? "" : `${hours} h`;
  const minutesStr = minutes === 0 ? "" : `${minutes} min`;
 
  return `${hoursStr} ${minutesStr}`;
};
 
export const formattedCurrentTime = (currentTime: Date): string => {
  const timeFormat = "HH:mm:ss";
  return dayjs(currentTime).tz(TIMEZONE).format(timeFormat);
};
 
/* eslint-enable no-restricted-syntax */