时间格式化的超好用插件

2,080 阅读2分钟

你是不是往往要对后端传的时间进行格式化展示?或者格式化成后端要的格式?那moment.js可以轻松搞定。

下面几种方式是我常用的几种,希望能够帮助到你们

moment().format('L');    // 2020/03/17
moment().format('l');    // 2020/3/17
moment().format('YYYY-MM-DD');    // 2020-3-17
moment().format('YYYY-MM-DD 00:00:00');    // 2020-3-17 00:00:00
moment().format('YYYY-MM-DD 23:59:59');    // 2020-3-17 23:59:59
moment().format('LL');   // 2020年3月17日
moment().format('ll');   // 2020年3月17日
moment().format('LLL');  // 2020年3月17日上午9点37分
moment().format('lll');  // 2020年3月17日 09:37
moment().format('LLLL'); // 2020年3月17日星期二上午9点37分
moment().format('llll'); // 2020年3月17日星期二 09:37
moment('2020-03-17').startOf('week').format('YYYY-MM-DD');    // 2020-03-15 3月17这周的开始是3月15 ,以此类推'month'/'year' 
moment().endOf("year/month/day/quarter/week");//某年/月/天/季度/周的结束日期
moment().valueOf()//现在时间的时间戳
moment('2020-01-12').valueOf()//2020-01-12号的时间戳,可以用来运算
moment().add(7, 'days');//今天加7天后的日期
moment().subtract(7, 'days');//今天减去7天后的日期
moment().format()//"2020-03-17T10:16:47+08:00"
  • moment().XXX代表对今日的操作

  • moment('2012-09-13').XXX代表对某日的操作

  • 更多用法请参考官网momentjs.cn/docs/#/i18n…

  • 使用前请安装moment

npm install moment//安装
import moment from "moment";//使用
moment().format();//页面中具体使用
  • 或下载moment.js并引入
<script src="moment.js"></script>
<script>
    moment().format();
</script>