微信小程序时间转换
mpvue微信小程序:时间转换 Android和IOS兼容问题
Android和IOS在时间解析时分隔符不同
1.Android使用‘-’来分割
2.IOS使用‘/’来分割
在ios中使用new Date('2019-06-17')返回的是null,ios在时间格式解析中不认识‘-’,需要替换为‘/’;字符串的方法中有str.replace()方法。
字符串str.replace()方法
1.str.replace('需要替换的字符串', "新字符串") // 这种方式只能替换字符串中首个需要替换的字符;如果后边还有想要替换的字符也不会进行替换
2.str.replace(/需要替换的字符串/g, "新字符串") // 使用正则表达式可以替换字符串中所有需要替换的字符(如果是‘-’、‘/’等特殊字符记得加‘反斜杠’哦!)。
formatDate(time) { // 接收参数格式 xxxx-xx-xx
let newTime = '';
let result = ''; // 接收时间
wx.getSystemInfo({ // 微信小程序获取设备系统信息
success:function(res){ // 获取系统信息成功
if(res.platform == "devtools"){
console.log('我是PC');
newTime = time;
}else if(res.platform == "ios"){
console.log('我是IOS');
newTime = time.replace(/-/g, '/'); // 系统为IOS时用‘/’来分割
}else if(res.platform == "android"){
console.log('我是android');
newTime = time;
}
let lr = new Date(newTime); // 传入的过去时间
let now = new Date(); // 当前时间
let dt = now -lr;
let second = dt / 1000; // 秒
if(second < 3600) {
result = parseInt(s / 60) + '分钟';
} else if(second < 86400) {
result = parseInt(s / 60 / 60) + '小时';
} else if(second <604800) {//在一周内
result = parseInt(s / 60 / 60 / 24) + '天';
} else if(second <31104000) {
result = parseInt(s / 60 / 60 / 24 / 30) + '月';
} else if(second <311040000) {
result = parseInt(s / 60 / 60 / 24 / 30 / 12) + '年';
}
}, fail(err){
console.log('设备系统信息获取失败', err)
},
})
return result
}这是我在使用mpvue开发微信小程序时遇到的一个时间转换的问题,希望能对ni有所帮助;如果有什么以后我们可以多多交流。
相关推荐
好好学习天天 2020-05-08
jyj0 2020-10-27
penkgao 2020-10-13
fylong 2020-09-11
贵州腾讯社交广告 2020-09-10
Haines 2020-06-16
flyingbird 2020-06-14
xzkjgw 2020-06-13
yourFriend 2020-06-09
songfens 2020-06-08
绿豆饼 2020-06-03
浪味仙 2020-06-03
heqiang0 2020-06-01
MIKUScallion 2020-05-25