FLYNNLABAboutMeCodingActivityStudy 2024초등수학
momentjs
javascript, momentjs

javascript Date 객체가 사용하다보면 참 불편하다.
Date객체의 불편함을 한방에 해결해주는 라이브러리가 있어서 소개한다.
momentjs이다.

http://momentjs.com/ http://momentjs.com/docs

Format Dates

moment().format('MMMM Do YYYY, h:mm:ss a'); // June 12th 2015, 7:49:37 am
moment().format('dddd');                    // Friday
moment().format("MMM Do YY");               // Jun 12th 15
moment().format('YYYY [escaped] YYYY');     // 2015 escaped 2015
moment().format();                          // 2015-06-12T07:49:37+09:00

Relative Time

moment("20111031", "YYYYMMDD").fromNow(); // 4 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 3 years ago
moment().startOf('day').fromNow();        // 8 hours ago
moment().endOf('day').fromNow();          // in 16 hours
moment().startOf('hour').fromNow();       // an hour ago

Calendar Time

moment().subtract(10, 'days').calendar(); // 06/02/2015
moment().subtract(6, 'days').calendar();  // Last Saturday at 7:50 AM
moment().subtract(3, 'days').calendar();  // Last Tuesday at 7:50 AM
moment().subtract(1, 'days').calendar();  // Yesterday at 7:50 AM
moment().calendar();                      // Today at 7:50 AM
moment().add(1, 'days').calendar();       // Tomorrow at 7:50 AM
moment().add(3, 'days').calendar();       // Monday at 7:50 AM
moment().add(10, 'days').calendar();      // 06/22/2015

Multiple Locale Support

// en
moment().format('L');    // 06/12/2015
moment().format('l');    // 6/12/2015
moment().format('LL');   // June 12, 2015
moment().format('ll');   // Jun 12, 2015
moment().format('LLL');  // June 12, 2015 7:51 AM
moment().format('lll');  // Jun 12, 2015 7:51 AM
moment().format('LLLL'); // Friday, June 12, 2015 7:51 AM
moment().format('llll'); // Fri, Jun 12, 2015 7:51 AM
// ko
moment().format('L');    // 2015.06.12
moment().format('l');    // 2015.6.12
moment().format('LL');   // 2015년 6월 12일
moment().format('ll');   // 2015년 6월 12일
moment().format('LLL');  // 2015년 6월 12일 오전 7시 51분
moment().format('lll');  // 2015년 6월 12일 오전 7시 51분
moment().format('LLLL'); // 2015년 6월 12일 금요일 오전 7시 51분
moment().format('llll'); // 2015년 6월 12일 금 오전 7시 51분