moment插件的使用姿势


moment插件的使用姿势

1. 日期格式化

moment().format('MMMM Do YYYY, h:mm:ss a'); // 三月 7日 2020, 11:59:47 中午
 
moment().format('dddd');                    // 星期六
 
moment().format("MMM Do YY");               // 3月 7日 20
 
moment().format('YYYY [escaped] YYYY');     // 2020 escaped 2020

moment().format();                          // 2020-03-07T11:59:47+08:00

2. 相对时间

moment("20111031", "YYYYMMDD").fromNow(); // 8 年前

moment("20120620", "YYYYMMDD").fromNow(); // 8 年前

moment().startOf('day').fromNow();        // 12 小时前

moment().endOf('day').fromNow();          // 12 小时内

moment().startOf('hour').fromNow();       // 1 小时前

3. 日历时间

moment().subtract(10, 'days').calendar(); // 2020/02/26

moment().subtract(6, 'days').calendar();  // 上星期日11:59

moment().subtract(3, 'days').calendar();  // 上星期三11:59

moment().subtract(1, 'days').calendar();  // 昨天11:59

moment().calendar();                      // 今天11:59

moment().add(1, 'days').calendar();       // 明天11:59

moment().add(3, 'days').calendar();       // 下星期二11:59

moment().add(10, 'days').calendar();      // 2020/03/17

4. 其他格式

moment.locale();         // zh-cn

moment().format('LT');   // 11:59
 
moment().format('LTS');  // 11:59:47

moment().format('L');    // 2020/03/07

moment().format('l');    // 2020/3/7

moment().format('LL');   // 2020年3月7日

moment().format('ll');   // 2020年3月7日

moment().format('LLL');  // 2020年3月7日中午11点59分

moment().format('lll');  // 2020年3月7日 11:59

moment().format('LLLL'); // 2020年3月7日星期六中午11点59分

moment().format('llll'); // 2020年3月7日星期六 11:59

5. 常用格式

moment().format("YYYY年MM月DD日 HH时mm分ss秒"); //2020年04月20日 18时59分50秒

moment(1711641720000).format('YYYY-MM-DD HH:mm:ss'); //2020-04-20 18:59:50(24小时制)
 
moment(1711641720000).format('YYYY-MM-DD hh:mm:ss'); //2020-04-20 06:59:50(12小时制)

moment 文档


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM