사용자 도구

사이트 도구


wiki:javascript:momentjs

moment.js, moment-with-locales.js

  • description : moment.js 사용법 정리
  • author : 주레피
  • email : dhan@repia.com
  • lastupdate : 2022-04-13

Intro

자바스크립트 날짜 관련 라이브러리

Install

Case Study

초를 읽기 쉬운 형태로 변환

function formattedTime(seconds) {
  // 경과한 시 분 초를 구함
  var formattedTime = moment
    .utc(moment.duration(seconds, "s").asMilliseconds())
    .format("HH시 mm분 ss초");
 
  // 경과한 날짜를 구함
  var formattedDay = parseInt(
    moment.utc(moment.duration(seconds, "s").asMilliseconds()).format("DDD")
  );
 
  // 날짜의 경우 시작을 1일로 하기 때문에 하루를 빼야 함
  var formatted = formattedDay - 1 + '일 ' + formattedTime;
 
  // 0일, 00분, 00시, 00초 는 표시하지 않음
  return formatted.replace(/0일/, "").replace(/00.?/g, "");
}
 
var seconds = 180304;
console.log(seconds, ' -> ', formattedTime(seconds));
 
-----------------------------------------------------
180304, ->, 2일 02시 05분 04초
1년이 지나면 년도가 올라가고 일은 초기화 되므로 사용하면 안됨.

Term

Tip

Troubleshooting

Ref

/var/services/web/dokuwiki/data/pages/wiki/javascript/momentjs.txt · 마지막으로 수정됨: 2023/01/13 18:44 (바깥 편집)