uniapp 0时区转当前时区 字符串转字符串

0时区转当前时区 字符串转字符串
话不多说直接上干货

/// 0时区转当前时区 字符串转字符串 
export function formatDateZeroToLocal(compareTime) {
	if (compareTime == null || compareTime.length <= 0) {
		return ""
	}
	const timeZoneOffset = new Date().getTimezoneOffset();
	const timeZoneHours = Math.floor(Math.abs(timeZoneOffset) / 60);
	const timeZoneMinutes = Math.abs(timeZoneOffset) % 60;
	var updateTimeData = timeProcessing(compareTime)
	// 设定目标时区差
	var targetZoneOffset = 60 * 60 * 1000; // 单位为ms
	if (timeZoneOffset < 0) { // 东半球
		updateTimeData += targetZoneOffset * timeZoneHours
	} else { // 西半球
		updateTimeData -= targetZoneOffset * timeZoneHours
	}
	let newTime = formatDateStan(updateTimeData)
	console.log("世界时:" + compareTime + ",----当前时:" + newTime)
	return newTime
}


/// 时间戳转时间字符串
export function formatDateStan(compareTime) {
	let date = new Date(compareTime);
	//时间戳为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 + ' ' + h + ':' + m + ':' + s;
}

打印结果
uniapp 0时区转当前时区 字符串转字符串_第1张图片

你可能感兴趣的:(uni-app,javascript,前端)