/**
* 时间戳转日期格式
* @param {Number} timestamp 时间戳
*/
export function formatterTimeYMDHM (timestamp) {
if (!timestamp) return ''
const localDate = new Date(timestamp);
const offset = localDate.getTimezoneOffset();
//date.getTimezoneOffset() 方法获取本地时间的时区偏移量,并将其转换为毫秒数。最后,我们将时区偏移量加到时间戳上,得到一个包含时区信息的本地时间戳 localTimestamp,并使用 new Date(localTimestamp) 方法将其转换为一个包含时区信息的日期对象 localDate。
const localTimestamp = timestamp + (offset + 480) * 60 * 1000;
//480是以北京时间获取的时间偏移量
let date = new Date(localTimestamp);
// var date = new Date(timestamp)
var year = date.getFullYear()
var mon = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
var minu = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
return year + '-' + mon + '-' + day + ' ' + hours + ':' + minu
}
js时间戳转成日期 需要解决各国时区问题的方法
最新推荐文章于 2025-01-02 10:45:00 发布