推薦一個 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分


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM