1、安装:
npm install moment --save # npm
yarn add moment # Yarn
2、转化格式:
moment().format(‘MMMM Do YYYY, h:mm:ss a‘); // 四月 7日 2020, 6:10:23 晚上
moment().format(‘dddd‘); // 星期二
moment().format("MMM Do YY"); // 4月 7日 20
moment().format(‘YYYY [escaped] YYYY‘); // 2020 escaped 2020
moment().format(); // 2020-04-07T18:10:23+08:00
moment("20111031", "YYYYMMDD").fromNow(); // 8 年前
moment("20120620", "YYYYMMDD").fromNow(); // 8 年前
moment().startOf(‘day‘).fromNow(); // 18 小时前
moment().endOf(‘day‘).fromNow(); // 6 小时内
moment().startOf(‘hour‘).fromNow(); // 10 分钟前
moment().subtract(10, ‘days‘).calendar(); // 2020/03/28
moment().subtract(6, ‘days‘).calendar(); // 上星期三18:10
moment().subtract(3, ‘days‘).calendar(); // 上星期六18:10
moment().subtract(1, ‘days‘).calendar(); // 昨天18:10
moment().calendar(); // 今天18:10
moment().add(1, ‘days‘).calendar(); // 明天18:10
moment().add(3, ‘days‘).calendar(); // 下星期五18:10
moment().add(10, ‘days‘).calendar(); // 2020/04/17
moment.locale(); // zh-cn
moment().format(‘LT‘); // 18:10
moment().format(‘LTS‘); // 18:10:23
moment().format(‘L‘); // 2020/04/07
moment().format(‘l‘); // 2020/4/7
moment().format(‘LL‘); // 2020年4月7日
moment().format(‘ll‘); // 2020年4月7日
moment().format(‘LLL‘); // 2020年4月7日晚上6点10分
moment().format(‘lll‘); // 2020年4月7日 18:10
moment().format(‘LLLL‘); // 2020年4月7日星期二晚上6点10分
moment().format(‘llll‘); // 2020年4月7日星期二 18:10
原文:https://www.cnblogs.com/ding-dong/p/12655113.html