使用时区缩写将一个时区中的日期转换为另一个时区

Convert date in one timezone to another timezone using timezone abbreviations

本文关键字:时区 日期 另一个 一个 转换 缩写      更新时间:2023-09-26

我正在研究一个web应用程序,用户可以在应用程序中设置他/她的时区,这在应用程序中进一步用于各种日期-时间转换。选择的时区可以不同于用户的本地时区。

我目前卡住了一个问题,我需要比较用户选择的日期(用户假设在他/她在应用程序中选择的时区)与当前日期,以查看所选日期是否为将来的日期。参考我搜索过的所有地方,我发现我可以在用户区域设置或UTC时间中获得当前日期。

所以我的问题的要点是-是否有任何方法转换日期从一个时区到另一个使用时区缩写?

我已经搜索了很多之前张贴在这里,但无法得到任何解决方案。我在搜索过程中发现的大多数地方都表明没有这样的解决方案。

我尝试过使用date.js,但它不满足目的,因为它是相当过时的,而且它支持的时区缩写是一个非常有限的集合。我也看了一下timezoneJS,但我不认为它适用于时区缩写。

是否有任何方法可以使用javascript或jquery完成?

给你:

// calculate local time in a different city given the city's UTC offset
function calcTime(city, offset) {
    // create Date object for current location
    var date = new Date();
    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    var utc = date.getTime() + (date.getTimezoneOffset() * 60000);
    // create new Date object for different city
    // using supplied offset
    var newDate = new Date(utc + (3600000 * offset));
    // return time as a string
    return "The local time in " + city + " is " + newDate.toLocaleString();
}
// get Bombay time
console.log(calcTime('Bombay', '+5.5'));
// get Singapore time
console.log(calcTime('Singapore', '+8'));
// get London time
console.log(calcTime('London', '+1'));