uni-app时间格式转换
已有时间戳直接看代码export default {data() {return {time:'1598346851',}},onLoad:function(){this.time = this.happenTimeFun(this.time);},methods: {happenTimeFun(num){//时间戳数据处理let date = new Date(num * 1000);//时间戳
·
已有时间戳
直接看代码
export default {
data() {
return {
time:'1598346851',
}
},
onLoad:function(){
this.time = this.happenTimeFun(this.time);
},
methods: {
happenTimeFun(num){//时间戳数据处理
let date = new Date(num * 1000);
//时间戳为10位需*1000,时间戳为13位的话不需乘1000
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? ('0' + MM) : MM;//月补0
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;//天补0
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;//小时补0
let m = date.getMinutes();
m = m < 10 ? ('0' + m) : m;//分钟补0
let s = date.getSeconds();
s = s < 10 ? ('0' + s) : s;//秒补0
return y + '-' + MM + '-' + d; //年月日
//return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s; //年月日时分秒
},
}
}
可以看到是通过方法 happenTimeFun 内部实现的日期格式转换,
获取当前时间
如果是需要获取现在的时间进行格式转换,可以获取当前时间:
happenTimeFun(){
let date = new Date();
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? ('0' + MM) : MM;//月补0
return MM +'/' + y;
},
使用:
this.time= this.happenTimeFun();
结果:
我这边获取的是当前时间。
更多推荐
已为社区贡献9条内容
所有评论(0)