Javascript countdown using absolute timezone?

前端 未结 6 1595
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 17:19

I have a javascript countdown timer that works by taking a specified date and time, and comparing it to the current date and time. The issue is, the current time is relative

6条回答
  •  不思量自难忘°
    2020-12-01 17:32

    A quick search reveals: convert-the-local-time-to-another-time-zone-with-this-javascript

    Following the article verbatim gets you this example:

    var d = new Date();
    
    var localTime = d.getTime();
    
    var localOffset = d.getTimezoneOffset() * 60000;
    
    var utc = localTime + localOffset;
    
    // obtain and add destination's UTC time offset
    // for example, Bombay 
    // which is UTC + 5.5 hours
    var offset = 5.5;   
    var bombay = utc + (3600000*offset);
    
    var nd = new Date(bombay); 
    alert("Bombay time is " + nd.toLocaleString() + "
    ");

    jsFiddle: http://jsfiddle.net/GEpaH/

    Just update with your desired offset and you should be all set.

提交回复
热议问题