Vue 8 hours to solve the axios submission time type time zone problem

Vue submits with axios, it will be 8 hours less problem,
solve the axios submission time type time zone problem, qs.stringify() time zone problem

When submitting with axios, qs.stringify() is required for serialization. The default time format of qs is

Date.prototype.toISOString;//Returned is Greenwich Mean Time 8 hours later than Beijing Time. You
need to modify the default date formatting method

The most classic Huagong Dafa, we directly changed the default method

//moment 是需要安装的 
// npm install moment
// 一次性解决时区和日期格式问题
import moment from 'moment'
Date.prototype.toISOString = function(){
    
     
    return moment(this).format('YYYY-MM-DD HH:mm:ss')
} 
// 一次性解决时区和日期格式问题

//The following is serialized in Qs. I don't recommend this. It doesn't make much sense. The above is enough. But for users who use Qs and don't want to modify the prototype, it still makes sense.

//第一个参数为需要序列化的数据
//第二个参数为配置选项
Qs.stringify(data, {
    
    
  serializeDate: (date) => {
    
    
      //用moment处理日期比较方便,自己写格式化方法也可以
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
  }
})

Guess you like

Origin blog.csdn.net/phker/article/details/112062307