如何使用不同时区处理日期然后将其转换为 UTC?
How to manipulate date with a different timezone then convert it to UTC?
所以,我住在菲律宾 (GMT+8) 我现在的时间是 June 1, 2020, 3:20 PM
当前UTC时间是June 1 2020, 7:20 AM
。
我想要它,所以我可以使用 Pacific/Honolulu
(GMT-10) 和当前时间 31 May 2020, 9:20 PM
来操纵日期,例如:
const date = new Date();
date.setDate(l.getDate() + 1)
console.log(date)
// ...further manipulation of date
date.getTime() //unix timestamp
这将显示当地时区,即 GMT+8。
所以我想选择一个时区,操纵日期,然后获取该操纵日期的 UTC 时间戳。
我尝试了各种方法,例如先将其转换为 UTC,但没有成功 - 似乎仍然找不到任何解决方法。
我在我的项目中使用 timezone-support and date-fns(如果这有帮助的话)(抱歉我不能使用 moment js),因为该项目已经相当大并且使用 date-fns 很长时间了。
所以我最终这样做是因为我使用的是 timezone-support:
首先我导入并初始化了数据:
import {
populateTimeZones,
listTimeZones,
getZonedTime,
findTimeZone,
getUnixTime,
setTimeZone,
convertTimeToDate,
} from 'timezone-support/dist/lookup-convert';
import timezoneData from 'timezone-support/dist/data-2012-2022';
// get the timezone support ready
populateTimeZones(timezoneData);
然后我创建了这个辅助函数:
const dateConvertToTimezone = (fromTimeZone, toTimeZone, date = new Date()) => {
const tz = findTimeZone(fromTimeZone);
const time = {
hours: date.getHours(),
minutes: date.getMinutes(),
day: date.getDate(),
month: date.getMonth() + 1,
year: date.getFullYear(),
};
// set the timezone to get the time object for the selected timezone
const selectedTime = setTimeZone(time, tz);
// convert the timezone to local one
const convertedToLocal = convertTimeToDate(selectedTime);
const utc = findTimeZone(toTimeZone);
const convertedTime = getZonedTime(convertedToLocal, utc);
return convertedTime;
};
然后我可以这样做:
const selectedTime = dateConvertToTimezone(
'Etc/UTC',
'Pacific/Honolulu',
new Date('May 31 2020, 21:20')
);
这将 return 火奴鲁鲁时间的 UTC,而不是我的本地时间。
所以,我住在菲律宾 (GMT+8) 我现在的时间是 June 1, 2020, 3:20 PM
当前UTC时间是June 1 2020, 7:20 AM
。
我想要它,所以我可以使用 Pacific/Honolulu
(GMT-10) 和当前时间 31 May 2020, 9:20 PM
来操纵日期,例如:
const date = new Date();
date.setDate(l.getDate() + 1)
console.log(date)
// ...further manipulation of date
date.getTime() //unix timestamp
这将显示当地时区,即 GMT+8。
所以我想选择一个时区,操纵日期,然后获取该操纵日期的 UTC 时间戳。
我尝试了各种方法,例如先将其转换为 UTC,但没有成功 - 似乎仍然找不到任何解决方法。
我在我的项目中使用 timezone-support and date-fns(如果这有帮助的话)(抱歉我不能使用 moment js),因为该项目已经相当大并且使用 date-fns 很长时间了。
所以我最终这样做是因为我使用的是 timezone-support:
首先我导入并初始化了数据:
import {
populateTimeZones,
listTimeZones,
getZonedTime,
findTimeZone,
getUnixTime,
setTimeZone,
convertTimeToDate,
} from 'timezone-support/dist/lookup-convert';
import timezoneData from 'timezone-support/dist/data-2012-2022';
// get the timezone support ready
populateTimeZones(timezoneData);
然后我创建了这个辅助函数:
const dateConvertToTimezone = (fromTimeZone, toTimeZone, date = new Date()) => {
const tz = findTimeZone(fromTimeZone);
const time = {
hours: date.getHours(),
minutes: date.getMinutes(),
day: date.getDate(),
month: date.getMonth() + 1,
year: date.getFullYear(),
};
// set the timezone to get the time object for the selected timezone
const selectedTime = setTimeZone(time, tz);
// convert the timezone to local one
const convertedToLocal = convertTimeToDate(selectedTime);
const utc = findTimeZone(toTimeZone);
const convertedTime = getZonedTime(convertedToLocal, utc);
return convertedTime;
};
然后我可以这样做:
const selectedTime = dateConvertToTimezone(
'Etc/UTC',
'Pacific/Honolulu',
new Date('May 31 2020, 21:20')
);
这将 return 火奴鲁鲁时间的 UTC,而不是我的本地时间。