在使用Element UI的过程中,发现DateTimePicker选择器返回值是GMT格式的,但向后端发送的时间格式是标准时间格式,于是需要把DateTimePicker返回的值转换格式
1.GMT转普通格式
GMTToStr(time){
let date = new Date(time)
let Str=date.getFullYear() + '-' +
(date.getMonth() + 1) + '-' +
date.getDate() + ' ' +
date.getHours() + ':' +
date.getMinutes() + ':' +
date.getSeconds()
return Str
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2.普通格式转GMT
StrToGMT(time){
let GMT = new Date(time)
return GMT
}
- 1
- 2
- 3
- 4
测试:
Print(){
let DateTime='Thu Jun 22 2017 19:07:30 GMT+0800'
let a=this.GMTToStr(DateTime)
console.log(a)
}
- 1
- 2
- 3
- 4
- 5
输出结果:
Print(){
let DateTime='2017-6-22 19:7:30'
let a=this.StrToGMT(DateTime)
console.log(a)
}
- 1
- 2
- 3
- 4
- 5
输出结果:
转载来源 http://blog.csdn.net/shuaizi96/article/details/73610599