Skip to content

Utils - Date

@teamsparta/utils에서 제공하는 날짜 관련 유틸리티 함수입니다.


calculateAge

국제적으로 통용되는 만 나이를 계산합니다. UTC 기준으로 정확하게 계산합니다.

API

typescript
function calculateAge(target: Date, standard?: Date): number;
// standard 기본값: new Date()

사용 예시

typescript
import { calculateAge } from '@teamsparta/utils';

const birthday = new Date('1995-03-15');
const age = calculateAge(birthday); // 현재 날짜 기준 만 나이

// 특정 기준일 지정
const ageAt2024 = calculateAge(birthday, new Date('2024-01-01')); // 28

getDateDiff

두 날짜의 차이를 일, 시, 분, 초로 반환합니다.

API

typescript
function getDateDiff(startDate: Date, endDate: Date): {
  days: number;
  hours: number;
  minutes: number;
  seconds: number;
};

사용 예시

typescript
import { getDateDiff } from '@teamsparta/utils';

const start = new Date('2024-01-01');
const end = new Date('2024-01-03T12:30:00');
const diff = getDateDiff(start, end);
// { days: 2, hours: 12, minutes: 30, seconds: 0 }

getTimePastText

주어진 날짜로부터 현재까지 경과한 시간을 한국어 텍스트로 반환합니다.

API

typescript
function getTimePastText(date: Date | string): string;

반환값 규칙:

  • 1분 미만: "방금 전"
  • 1시간 미만: "n분 전"
  • 1일 미만: "n시간 전"
  • 1달 미만: "n일 전"
  • 1년 미만: "n달 전"
  • 1년 이상: "n년 전"

사용 예시

typescript
import { getTimePastText } from '@teamsparta/utils';

getTimePastText(new Date(Date.now() - 30000));    // "방금 전"
getTimePastText(new Date(Date.now() - 3600000));  // "60분 전"
getTimePastText('2024-01-01T00:00:00');           // "n달 전" 또는 "n년 전"

getNewDate

Date 객체를 복사하고 변환 함수를 적용한 새로운 Date 객체를 반환합니다.

API

typescript
function getNewDate(date: Date | string, dateFn: (date: Date) => void): Date;

사용 예시

typescript
import { getNewDate } from '@teamsparta/utils';

const tomorrow = getNewDate(new Date(), (d) => d.setDate(d.getDate() + 1));
const nextMonth = getNewDate('2024-01-15', (d) => d.setMonth(d.getMonth() + 1));

fixServerDate

서버에서 받은 한국시간(UTC+0 형식) 데이터를 실제 UTC로 정규화합니다. DB에 KST로 저장되었지만 UTC+0 형식으로 표현된 날짜를 보정합니다.

API

typescript
function fixServerDate(date: Date | string): Date;

사용 예시

typescript
import { fixServerDate } from '@teamsparta/utils';

// 서버에서 받은 데이터 (한국시간 9시이지만 UTC+0로 표현됨)
const serverDate = '2024-01-01T09:00:00+00:00';
const fixedDate = fixServerDate(serverDate);
console.log(fixedDate.toISOString()); // "2024-01-01T00:00:00.000Z"

isBeforeOrEqual

첫 번째 날짜가 두 번째 날짜보다 이전 또는 동일한지 확인합니다.

API

typescript
function isBeforeOrEqual(date1: Date, date2: Date): boolean;

사용 예시

typescript
import { isBeforeOrEqual } from '@teamsparta/utils';

isBeforeOrEqual(new Date('2024-01-01'), new Date('2024-01-02')); // true
isBeforeOrEqual(new Date('2024-01-01'), new Date('2024-01-01')); // true
isBeforeOrEqual(new Date('2024-01-02'), new Date('2024-01-01')); // false

isAfterOrEqual

첫 번째 날짜가 두 번째 날짜보다 이후 또는 동일한지 확인합니다.

API

typescript
function isAfterOrEqual(date1: Date, date2: Date): boolean;

사용 예시

typescript
import { isAfterOrEqual } from '@teamsparta/utils';

isAfterOrEqual(new Date('2024-01-02'), new Date('2024-01-01')); // true
isAfterOrEqual(new Date('2024-01-01'), new Date('2024-01-01')); // true
isAfterOrEqual(new Date('2024-01-01'), new Date('2024-01-02')); // false