使用时区缩写将一个时区中的日期转换为另一个时区 [英] Convert date in one timezone to another timezone using timezone abbreviations

查看:122
本文介绍了使用时区缩写将一个时区中的日期转换为另一个时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,用户可以在应用程序中设置他/她的时区,该应用程序在应用程序中进一步用于各种日期时间转换。所选时区可能与用户的区域设置时区不同。

I am working on a web application where a user can set his/her timezone in the application which is further used in the application for various date-time conversions. The selected timezone can be different from user's locale timezone.

我目前遇到的问题是我需要比较用户选择的日期(用户假定在哪个日期)他/她在申请中选择的时区)和当前日期,以查看所选日期是否是将来的日期。参考我搜索过的所有地方,我发现我可以在用户区域设置或UTC时间获取当前日期。

I am currently stuck on a problem where I need to compare a user selected date(which user assumes to be in the timezone selected by him/her in the application) with the current date to see if the selected date is a date in future. With reference to all the places I have searched, I have found that I can get current date in user locale or UTC time.

所以我的问题的要点是 - 有没有办法使用时区缩写将日期从一个时区转换为另一个时区?

So the gist of my problem is - Is there any way to convert a date from one timezone to another using the timezone abbreviations?

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

I have searched a lot before posting here but could not get any solution. Most of the places that I found during my search suggest that there is no such solution available.

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

I have tried using date.js but it does not suffice the purpose as it is quite outdated and also the timezone abbreviations supported by it is a very limited set. I have also taken a look a timezoneJS but I don't think that it works with timezone abbreviations.

有没有办法可以用javascript或jquery来完成它?

Is there any way in which it can be accomplished using javascript or 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'));

这篇关于使用时区缩写将一个时区中的日期转换为另一个时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆