例子:项目获取到的时间在东二区,用户在东八区,那么要把东二区的时间转换成东八区的时间
时区可在pc上设置
//转换当前时区的时间 兼容ios、时间戳
export function convertTureTime(time){let nDate = new Date();//当前时间let y = nDate.getUTCFullYear(); let mm = nDate.getUTCMonth(); let d = nDate.getUTCDate(); let h = nDate.getUTCHours(); let m = nDate.getUTCMinutes(); let s = nDate.getUTCSeconds(); let UTCDate = new Date(y,mm,d,h,m,s).getTime();//标准时间 时间戳 伦敦let nowDate = Math.trunc(new Date() / 1000) * 1000 //忽略毫秒let subDate = mathSub(nowDate,UTCDate) //当前时间与标准时间相差多少 减法运算let australiaDate = mathMul(2,3600000) //东二区2小时的时间戳,根据实际情况来确定 乘法运算let subTime = mathSub(subDate,australiaDate) //东二区与当前时区相差多少时间戳let timeDate;if(time.toString().length == 10){timeDate = mathMul(time,1000) }else if(time.toString().length == 13){timeDate = time}else {timeDate = new Date(time.replace(/-/g,'/')).getTime() //该时间的时间戳 }let rTime = mathAdd(timeDate,subTime) //该时间戳 + 相差时间戳 = 该时间在当前时区的时间戳return rTime;
}
项目用的是uview,所以在接收到时间戳还要在转换成年月日格式,这个就在外面去操作,写在convertTureTime里还要好多判断