我正在寻找一个函数转换日期在一个时区到另一个。
它需要两个参数,
日期(格式为“2012/04/10 10:10:30 +0000”) 时区字符串("Asia/Jakarta")
时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述
有什么简单的方法吗?
我正在寻找一个函数转换日期在一个时区到另一个。
它需要两个参数,
日期(格式为“2012/04/10 10:10:30 +0000”) 时区字符串("Asia/Jakarta")
时区字符串在http://en.wikipedia.org/wiki/Zone.tab中描述
有什么简单的方法吗?
当前回答
如果你有一个时区偏移,你可以使用这个:
const timezoneDate = (timestamp: number, offsetInHours: number) => {
const shiftedTimestam = timestamp + offsetInHours * 3600000;
const shiftedDate = new Date(shiftedTimestam);
return {
date: shiftedDate.getUTCDate(),
hours: shiftedDate.getUTCHours(),
minutes: shiftedDate.getUTCMinutes(),
// ...other UTC methods
}
};
// Usage:
const newYorkDate = timezoneDate(Date.now(), -5);
console.log(
`It is ${newYorkDate.hours}:${newYorkDate.minutes} in NYC`
);
其他回答
环顾四周,包括这个页面的链接,我发现了这篇很棒的文章,使用moment timezone:
https://www.webniraj.com/2016/11/23/javascript-using-moment-js-to-display-dates-times-in-users-timezone/
总结一下:
获取用户的时区
var tz = moment.tz.guess();
console.info('Timezone: ' + tz);
返回时区:欧洲/伦敦
设置默认用户时区
moment.tz.setDefault(tz);
设置自定义时区
moment.tz.setDefault('America/Los_Angeles');
将日期/时间转换为本地时区,假设原始日期/时间为UTC
moment.utc('2016-12-25 07:00').tz(tz).format('ddd, Do MMMM YYYY, h:mma');
返回时间:2016年12月25日,星期日,上午7:00
将日期/时间转换为洛杉矶时间
moment.utc('2016-12-25 07:00').tz('America/Los_Angeles').format('ddd, Do MMMM YYYY, h:mma');
返回时间:2016年12月24日星期六晚上11:00
将洛杉矶时间转换为伦敦时间
moment.tz('2016-12-25 07:00', 'America/Los_Angeles').tz('Europe/London').format( 'ddd, Do MMMM YYYY, h:mma' );
返回时间:2016年12月25日,星期日,下午3:00
下面是一行代码:
function convertTZ(date, tzString) { return new Date((typeof date === "string" ? new Date(date) : date).toLocaleString("en-US", {timeZone: tzString})); } // usage: Asia/Jakarta is GMT+7 convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") // Tue Apr 20 2012 17:10:30 GMT+0700 (Western Indonesia Time) // Resulting value is regular Date() object const convertedDate = convertTZ("2012/04/20 10:10:30 +0000", "Asia/Jakarta") convertedDate.getHours(); // 17 // Bonus: You can also put Date object to first arg const date = new Date() convertTZ(date, "Asia/Jakarta") // current date-time in jakarta.
这是MDN参考。
注意:上面的函数依赖于解析toLocaleString结果,这是一个以en-US语言环境格式化的日期字符串,例如。“4/20/2012,下午5:10:30”。每个浏览器可能不接受en-US格式的日期字符串到它的日期构造函数,它可能返回意想不到的结果(它可能忽略夏令时)。
目前所有现代浏览器都接受这种格式并正确计算夏令时,它可能不适用于旧浏览器和/或外来浏览器。
旁注:如果现代浏览器有tolocedate,那就太好了 函数,所以我们不需要使用这个俗套的方法。
时区当前时区的偏移量
date +%s -d '1 Jan 1970'
对于我所在的GMT+10时区(澳大利亚),它返回-36000
我应该指出,我在可以使用的外部库方面受到了限制。moment.js和timezone-js不是我的选择。
我拥有的js日期对象是UTC。我需要在特定的时区(在我的例子中是“America/Chicago”)从这个日期获得日期和时间。
var currentUtcTime = new Date(); // This is in UTC
// Converts the UTC time to a locale specific format, including adjusting for timezone.
var currentDateTimeCentralTimeZone = new Date(currentUtcTime.toLocaleString('en-US', { timeZone: 'America/Chicago' }));
console.log('currentUtcTime: ' + currentUtcTime.toLocaleDateString());
console.log('currentUtcTime Hour: ' + currentUtcTime.getHours());
console.log('currentUtcTime Minute: ' + currentUtcTime.getMinutes());
console.log('currentDateTimeCentralTimeZone: ' + currentDateTimeCentralTimeZone.toLocaleDateString());
console.log('currentDateTimeCentralTimeZone Hour: ' + currentDateTimeCentralTimeZone.getHours());
console.log('currentDateTimeCentralTimeZone Minute: ' + currentDateTimeCentralTimeZone.getMinutes());
国际标准时间目前比“美国/芝加哥”早6小时。输出是:
currentUtcTime: 11/25/2016
currentUtcTime Hour: 16
currentUtcTime Minute: 15
currentDateTimeCentralTimeZone: 11/25/2016
currentDateTimeCentralTimeZone Hour: 10
currentDateTimeCentralTimeZone Minute: 15
如果你有一个时区偏移,你可以使用这个:
const timezoneDate = (timestamp: number, offsetInHours: number) => {
const shiftedTimestam = timestamp + offsetInHours * 3600000;
const shiftedDate = new Date(shiftedTimestam);
return {
date: shiftedDate.getUTCDate(),
hours: shiftedDate.getUTCHours(),
minutes: shiftedDate.getUTCMinutes(),
// ...other UTC methods
}
};
// Usage:
const newYorkDate = timezoneDate(Date.now(), -5);
console.log(
`It is ${newYorkDate.hours}:${newYorkDate.minutes} in NYC`
);