js时间戳转成日期 需要解决各国时区问题的方法

/**
 * 时间戳转日期格式
 * @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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值