推荐一个 JavaScript 日期处理类库 Moment.js

官网: http://momentjs.com/

处理时间的展示,很方便。

安装

bower install moment --save # bower
npm install moment --save   # npm
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor

日期格式化

moment().format('MMMM Do YYYY, h:mm:ss a'); // 十一月 3日 2017, 9:29:25 上午
moment().format('dddd');                    // 星期五
moment().format("MMM Do YY");               // 11月 3日 17
moment().format('YYYY [escaped] YYYY');     // 2017 escaped 2017
moment().format();                          // 2017-11-03T09:29:25+08:00

相对时间

moment("20111031", "YYYYMMDD").fromNow(); // 6 年前
moment("20120620", "YYYYMMDD").fromNow(); // 5 年前
moment().startOf('day').fromNow();        // 9 小时前
moment().endOf('day').fromNow();          // 15 小时内
moment().startOf('hour').fromNow();       // 29 分钟前

日历时间

moment().subtract(10, 'days').calendar(); // 2017年10月24日
moment().subtract(6, 'days').calendar();  // 上周六上午9点29
moment().subtract(3, 'days').calendar();  // 本周二上午9点29
moment().subtract(1, 'days').calendar();  // 昨天上午9点29分
moment().calendar();                      // 今天上午9点29分
moment().add(1, 'days').calendar();       // 明天上午9点29分
moment().add(3, 'days').calendar();       // 下周一上午9点29
moment().add(10, 'days').calendar();      // 2017年11月13日

多语言支持

moment().format('L');    // 2017-11-03
moment().format('l');    // 2017-11-03
moment().format('LL');   // 2017年11月3日
moment().format('ll');   // 2017年11月3日
moment().format('LLL');  // 2017年11月3日上午9点29分
moment().format('lll');  // 2017年11月3日上午9点29分
moment().format('LLLL'); // 2017年11月3日星期五上午9点29分
moment().format('llll'); // 2017年11月3日星期五上午9点29分
原文地址:https://www.cnblogs.com/jshare/p/7776640.html