Use moment.js to calculate time in vue

1. Install moment.js

 npm install moment --save

2. Register as a global method in the main.js file

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

3. Use on the page

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 official document

moment.js Chinese website

Guess you like

Origin blog.csdn.net/qq_41839808/article/details/117520835