【重点突破】—— moment.js获取时间格式化

官网

 

安装

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor
bower install moment --save # bower (废弃)

日期格式化

moment().format('MMMM Do YYYY, h:mm:ss a'); // 十月 18日 2021, 3:43:45 下午
moment().format('dddd');                    // 星期一
moment().format("MMM Do YY");               // 10月 18日 21
moment().format('YYYY [escaped] YYYY');     // 2021 escaped 2021
moment().format();                          // 2021-10-18T15:43:45+08:00 

相对时间

moment("20111031", "YYYYMMDD").fromNow(); // 10 年前
moment("20120620", "YYYYMMDD").fromNow(); // 9 年前
moment().startOf('day').fromNow();        // 16 小时前
moment().endOf('day').fromNow();          // 8 小时内
moment().startOf('hour').fromNow();       // 44 分钟前

日历时间

moment().subtract(10, 'days').calendar(); // 2021/10/08
moment().subtract(6, 'days').calendar();  // 上星期二15:43
moment().subtract(3, 'days').calendar();  // 上星期五15:43
moment().subtract(1, 'days').calendar();  // 昨天15:43
moment().calendar();                      // 今天15:43
moment().add(1, 'days').calendar();       // 明天15:43
moment().add(3, 'days').calendar();       // 下星期四15:43
moment().add(10, 'days').calendar();      // 2021/10/28  

 

posted @ 2021-10-19 10:06  柳洁琼Elena  阅读(177)  评论(0编辑  收藏  举报