// 时间字符串转换时间戳
export function timeNum(time: string) {
let date = time.replace(/-/g, '/');
var timestamp = new Date(date).getTime();
return timestamp
}
// 时间戳转换字符串日期
export function timestr(num: number) {
var date = new Date(num); //根据时间戳生成的时间对象
let Month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
var time = (date.getFullYear()) + "-" + Month + "-" + day + " " + hours + ":" + minute + ":" + second;
return time
}
// 获取对应时区时间
// str:当前时区的时间 number:时区 例:UTC-1传(-1),UTC+5传(5),UTC传(0)
export function getTime(str: string, number: number) {
var date = new Date();
date.getTimezoneOffset(); //-480 (获取当前时区和柏林时区的时间差,单位分钟)
// 柏林时间
let bolin = timeNum(str) + (date.getTimezoneOffset() * 1000 * 60)
let time = bolin + (number * 1000 * 60 * 60)
console.log('柏林时间' + timestr(bolin), '当前传入时区时间' + timestr(time))
return timestr(time)
}
将时间转换为指定的时区时间,字符串时间和时间戳互相转换
最新推荐文章于 2024-08-16 11:41:16 发布