您好,欢迎来到星星旅游。
搜索
您的当前位置:首页vue中使用moment.js计算时间

vue中使用moment.js计算时间

来源:星星旅游

1. 安装moment.js

 npm install moment --save

2. 在main.js文件中注册为全局的方法

 import moment from 'moment'
Vue.prototype.$moment = moment

3. 在页面上使用

this.$moment().format('YYYY-MM-DD HH:mm:ss')  //当前时间
this.$moment().subtract(90, 'days').format('YYYY-MM-DD HH:mm:ss')  //当前时间往前90天
this.$moment().add(7, 'days').format('YYYY-MM-DD HH:mm:ss')   //当前时间往后推7天
this.$moment().subtract(1, "years").format("YYYY-MM-DD")  //当前时间的前1年时间
this.$moment().subtract(3, "months").format("YYYY-MM-DD") //当前时间的前3个月时间
this.$moment().subtract(1, "weeks").format("YYYY-MM-DD")  //当前时间的前一个星期时间
//计算两个时间之间的间隔
const [start_time, end_time] = ['2021-04-02 15:59:06', '2021-07-01 15:59:06']
//计算相差多少天,参数可传:day 、second、minute
let dateRange = this.$moment(end_time).diff(this.$moment(start_time), 'day')

4. moment.js官方文档

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- stra.cn 版权所有 赣ICP备2024042791号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务