【问题标题】:How to set the time for a specific local in javascript如何在javascript中设置特定本地的时间
【发布时间】:2010-05-18 07:31:20
【问题描述】:

我有一个问题,也许有人可以帮助我,我会解释...

  1. 我有 in javascript "var date= new date();"它给了我当地时间(浏览器时间),但我想强制这个数据/时间用于特定的本地......例如......西班牙。我希望每次有人(从其他国家/地区)进入页面时,日期必须是西班牙时间。

我找到了一些解决方案,但问题是夏令时和冬令时......我们有偏移变化,因为有些时间是 +1 小时,而其他时间是 +2......

谁能帮我解决一个问题?

谢谢 jrms_pnf@hotmail.com

【问题讨论】:

    标签: javascript date timezone locale


    【解决方案1】:

    看看这篇文章:https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html

    从那里直接升起:

     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();
    
     }
    

    这样称呼它:alert(calcTime('Bombay', '+5.5')); // get Bombay time

    您可能需要添加一些逻辑来考虑夏令时。

    【讨论】:

    • 谢谢,但我已经检查了这段代码,但我的问题是在这种情况下,你说差异是 5 小时半......每次,但有时或多或少(我不完全了解孟买:) 我该怎么做?硬编码逻辑? 2010 年?然后是 2011 年 .. 公元前可能每一年改变小时的日期都不同。谢谢:d
    • 我不知道有任何图书馆可以为您解决这个问题。 Google 为您自己编写逻辑返回了大量结果:google.co.uk/…
    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多