开发者

How to set the time for a specific local in javascript

开发者 https://www.devze.com 2022-12-30 10:16 出处:网络
i have a problem, a开发者_开发技巧nd maybe someone can help me, i will explain... i have the in javascript \"var date= new date();\" and its give me the local time (browser time) but i want force th

i have a problem, a开发者_开发技巧nd maybe someone can help me, i will explain...

  1. i have the in javascript "var date= new date();" and its give me the local time (browser time) but i want force this data/time for a especific local... for example... Spain. i want everytime that someone enter in the page (from others country) the date need be the spanish hour.

i found some soluction but the problem is the summer time and winter time... we have offset variations because some time is +1 hours and others is +2....

someone can help me in one soluction?

thanks jrms_pnf@hotmail.com


Take a look at this article: https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html

Lifted straight out from that:

 function calcTime(city, offset) {

// create Date object for current location
d = new Date();

// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);

// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));

// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();

 }

Call it like: alert(calcTime('Bombay', '+5.5')); // get Bombay time

You may need to add a bit of logic to take into account daylight saving time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号