0时区转当前时区 字符串转字符串
话不多说直接上干货文章来源:https://www.toymoban.com/news/detail-806365.html
/// 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;
}
打印结果
文章来源地址https://www.toymoban.com/news/detail-806365.html
到了这里,关于uniapp 0时区转当前时区 字符串转字符串的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!