All files / src/lib formatDateTime.ts

21.42% Statements 12/56
100% Branches 1/1
33.33% Functions 1/3
21.42% Lines 12/56

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 571x 2x 2x 2x 2x 2x 2x 1x 1x                                               1x 1x 1x                                            
export const formatISO = (dateString: string) => {
  const date = new Date(dateString);
  const y = date.getFullYear();
  const m = String(date.getMonth() + 1).padStart(2, '0');
  const d = String(date.getDate()).padStart(2, '0');
  return `${y}. ${m}. ${d}`;
};
 
export const formatTimeAgo = (isoString: string) => {
  const dateInput = new Date(isoString.endsWith('Z') ? isoString : `${isoString}Z`);
  const dateNow = new Date();

  if (dateInput.getTime() >= dateNow.getTime()) return '0초 전';

  const diffPerSec = Math.floor((dateNow.getTime() - dateInput.getTime()) / 1000);
  if (diffPerSec < 60) return `${diffPerSec}초 전`;

  const diffPerMin = Math.floor(diffPerSec / 60);
  if (diffPerMin < 60) return `${diffPerMin}분 전`;

  const diffPerHour = Math.floor(diffPerMin / 60);
  if (diffPerHour < 24) return `${diffPerHour}시간 전`;

  const diffPerDay = Math.floor(diffPerHour / 24);
  if (diffPerDay < 30) return `${diffPerDay}일 전`;

  const yearDiff = dateNow.getFullYear() - dateInput.getFullYear();
  const monthDiff = dateNow.getMonth() - dateInput.getMonth();
  const diffPerMonth = yearDiff * 12 + monthDiff;
  if (diffPerMonth < 12) return `${diffPerMonth}개월 전`;
  return `${yearDiff}년 전`;
};
 
// 모임 시작 시간을 Card 컴포넌트용 형식으로 변환 (예: "25. 12. 25 - 19:00")
export const formatDateTime = (startTime: string, customFormat?: string): string => {
  const start = new Date(startTime);

  const fullYear = start.getFullYear().toString();
  const shortYear = fullYear.slice(-2);
  const month = String(start.getMonth() + 1).padStart(2, '0');
  const day = String(start.getDate()).padStart(2, '0');
  const hours = String(start.getHours()).padStart(2, '0');
  const minutes = String(start.getMinutes()).padStart(2, '0');
  const seconds = String(start.getSeconds()).padStart(2, '0');

  if (!customFormat) return `${shortYear}. ${month}. ${day} - ${hours}:${minutes}`;

  return customFormat
    .replace(/yyyy/g, fullYear)
    .replace(/yy/g, shortYear)
    .replace(/MM/g, month)
    .replace(/dd/g, day)
    .replace(/HH/g, hours)
    .replace(/mm/g, minutes)
    .replace(/ss/g, seconds);
};