其他时区的时间转换成当前时区的时间

36 篇文章 0 订阅

例子:项目获取到的时间在东二区,用户在东八区,那么要把东二区的时间转换成东八区的时间
时区可在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里还要好多判断

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值