[javascript] 현재로부터 얼마나 지난 시간인지 계산 및 출력하기

원출처 : https://kdinner.tistory.com/68

const timeForToday = (value) => {
        value = value.replace(/-/g, "/"); // IOS 사파리에서 "2000-12-11 11:11:11" 형식 미지원 이슈 처리 20220403
        const today = new Date();
        const timeValue = new Date(value);

        const betweenTime = Math.floor((today.getTime() - timeValue.getTime()) / 1000 / 60);
        if (betweenTime < 1) return '방금전';
        if (betweenTime < 60) {
            return `${betweenTime}분전`;
        }

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

        const betweenTimeDay = Math.floor(betweenTime / 60 / 24);
        if (betweenTimeDay < 365) {
            return `${betweenTimeDay}일전`;
        }

        return `${Math.floor(betweenTimeDay / 365)}년전`;
 }

Leave a Comment