Moment.js日期处理库的使用
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 |
官方文档:
http://momentjs.cn/
moment.js下载:
http://cdn.staticfile.org/moment.js/2.24.0/moment.js
moment.js多语言下载:
http://cdn.staticfile.org/moment.js/2.24.0/moment-with-locales.js
引入
或
var moment = require('moment');moment().format();
日期格式化
moment().format('MMMM Do YYYY, h:mm:ss a'); // 十二月 16日 2021, 4:22:44 下午moment().format('dddd'); // 星期四moment().format("MMM Do YY"); // 12月 16日 21moment().format('YYYY [escaped] YYYY'); // 2021 escaped 2021moment().format(); // 2021-12-16T16:22:44+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(); // 23 分钟前
日历时间
moment().subtract(10, 'days').calendar(); // 2021/12/06moment().subtract(6, 'days').calendar(); // 上星期五16:22moment().subtract(3, 'days').calendar(); // 上星期一16:22moment().subtract(1, 'days').calendar(); // 昨天16:22moment().calendar(); // 今天16:22moment().add(1, 'days').calendar(); // 明天16:22moment().add(3, 'days').calendar(); // 下星期日16:22moment().add(10, 'days').calendar(); // 2021/12/26
多语言支持
moment.locale(); // zh-cnmoment().format('LT'); // 16:22moment().format('LTS'); // 16:22:44moment().format('L'); // 2021/12/16moment().format('l'); // 2021/12/16moment().format('LL'); // 2021年12月16日moment().format('ll'); // 2021年12月16日moment().format('LLL'); // 2021年12月16日下午4点22分moment().format('lll'); // 2021年12月16日 16:22moment().format('LLLL'); // 2021年12月16日星期四下午4点22分moment().format('llll'); // 2021年12月16日星期四 16:22

好啦,本期内容就分享到这里,我们下期见!
评论
