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官方文档

moment.js中文网

猜你喜欢

转载自blog.csdn.net/qq_41839808/article/details/117520835
今日推荐