TIL. D-day 구하기

FE_JACOB 👨🏻‍💻·2021년 11월 15일
0

Today I learned

목록 보기
28/30

D-day 구하기

회사 프로젝트 중

게시물이 작성된지 얼만큼 시간이 지났는지,
또 게시물이 없어지기까지 due date 가 얼마나 남았는지를 계산했다.

작성된 기간

const secondsPast = parseInt((new Date() - '작성된 날짜') / 1000, 10);


 switch (true) {
   case secondsPast < 60:
     return '방금';
   case secondsPast < 3600:
     return `${parseInt(secondsPast / 60, 10)}분 전`;
   case secondsPast < 86400:
     return `${parseInt(secondsPast / 3600, 10)}시간 전`;
   default:
     return `${parseInt(secondsPast / 86400, 10)}일 전`;
 }

작성할 수 있는 시간

const now = new Date()
const closeAt = new Date('D-day날짜')

const gap = now - closeAt 

const second = Math.floor((gap / 1000) % 60);
const minute = Math.floor((gap / 1000 / 60) % 60);
const hour = Math.floor(gap / 1000 / 60 / 60) % 24;
const day = Math.floor(gap / 1000 / 60 / 60 / 24);
profile
단순히 개발자로서 제게 있었던 일을 적는 공간입니다 ✍🏻

0개의 댓글