不同时区的倒数计时器

时间:2017-12-01 20:30:43

标签: javascript

我需要为公司办公室制定一个倒数计时器小时:

一个办事处在巴西,另一个办事处在日本

巴西= UTC-03:00 JAPAN = UTC + 09:00

两个办事处都在上午8点营业,晚上8点关闭,当巴西办事处开业时,日本办事处正在关闭。

我如何配置日语,看到巴西办公室关闭,日本办公室开放,反之亦然

点击链接:http://lickslegal.webflow.io/teste/relogio

我的javascript代码:

function getTimeRemaining(endtime){

  var t = Date.parse(endtime) - Date.parse(new Date());
  var seconds = Math.floor( (t/1000) % 60 );
  var minutes = Math.floor( (t/1000/60) % 60 );
  var hours = Math.floor( (t/(1000*60*60)) % 24 );
  var days = Math.floor( t/(1000*60*60*24) );
  return {
    'total': t,
    'days': days,
    'hours': hours,
    'minutes': minutes,
    'seconds': seconds
  };
}


function initializeClock(endtime, clock_location, status, add_class, remove_class){

  clock_location.find('.relogio_status').removeClass(remove_class);
  clock_location.find('.relogio_status').addClass(add_class);

  var timeinterval = setInterval(function(){
    var t = getTimeRemaining(endtime);

    var show_message = ' '+status+' in ';

    if (t.days > 0) {
        show_message += t.days+" d ";
    }

    show_message += (t.hours < 10 ? '0':'')+t.hours+":"+(t.minutes < 10 ? '0':'')+t.minutes+":"+(t.seconds < 10 ? '0':'')+t.seconds;

    clock_location.find('.status_clock_text').html(show_message);

    if(t.total==0){
      // clearInterval(timeinterval);
      // location.reload();
      clock_city('rio_de_janeiro');
    }
  },1000);
}


function when_monday(){
    var will_monday = new Date();
    will_monday.setDate(will_monday.getDate() + (1 + 7 - will_monday.getDay()) % 7);

    return {
        'year': will_monday.getFullYear(),
        'month': (will_monday.getMonth() +1),
        'date': will_monday.getDate(),
        'full': will_monday,
        'open_or_close': will_monday.getFullYear()+"-"+(will_monday.getMonth() +1)+"-"+will_monday.getDate()
    };
}

function go_tomorrow(){
    var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);

    return {
        'year': tomorrow.getFullYear(),
        'month': (tomorrow.getMonth() +1),
        'date': tomorrow.getDate(),
        'full': tomorrow,
        'open_or_close': tomorrow.getFullYear()+"-"+(tomorrow.getMonth() +1)+"-"+tomorrow.getDate()
    };
}


function clock_city(city_id){
    var today = new Date();

    var y = today.getFullYear();
    var m = (today.getMonth() +1);
    var d = today.getDate();

    var location_city = $("#"+city_id);

    var city_open_time = location_city.find('.open_time').text();
    var city_close_time = location_city.find('.close_time').text();
    var city_close_date = location_city.find('.close_date').text();

    // var city_open_deadline = y+"-"+m+"-"+d+" "+city_open_time;
    var city_close_deadline = y+"-"+m+"-"+d+" "+city_close_time;



    var hora_atual = (today.getHours() < 10 ? '0':'')+today.getHours()+':'+today.getMinutes();

    if (hora_atual >= city_open_time && hora_atual < city_close_time){
        //ABERTO
        console.log('aberto');
        initializeClock(city_close_deadline, location_city, 'closing', 'office_open', 'office_closed');
    }else{
        //FECHADO
        console.log('fechado');

        //Verifica se for SEXTA ou FDS
        if ( (today.getDay() +1) == 6 || today.getDay() == 6 || today.getDay() == 0) {  
            var city_open_deadline = when_monday().open_or_close+" "+city_open_time;
        }else{
            var city_open_deadline = go_tomorrow().open_or_close+" "+city_open_time;
        }
        initializeClock(city_open_deadline, location_city, 'opening', 'office_closed', 'office_open');
    }
}


$(document).ready(function() {
    clock_city('rio_de_janeiro');
    clock_city('tokyo');
});

2 个答案:

答案 0 :(得分:1)

部分问题是您的网站说巴西办公时间是08:00到20:00,日本办公时间是08:00到20:00。这些范围都不正确。巴西办公时间为11:00至23:00,日本办公时间为23:00至11:00。作为一般规则,除最终显示外,使用UTC。

rolling以UTC格式生成一个包含当前日期的Date对象。由于您的参考时间也是UTC,因此无需转换即可查找剩余时间。由于您实际上并不关心日期,只关注持续时间,因此您不需要对输出进行任何时区调整。

答案 1 :(得分:1)

UTC偏移量可以添加到当前的UTC日期和时间:

&#13;
&#13;
console.log( 'Z+0:',           new Date            )  // current UTC time
console.log( 'Z-3:', new Date( new Date -3 * 36e5) )  // current UTC time minus 3 hours
console.log( 'Z+8:', new Date(+new Date +8 * 36e5) )  // + used to convert to number

console.log( 'Tests:' )
console.log( 'Z+0:',           new Date(0)            )  // "1970-01-01T00:00:00.000Z"
console.log( 'Z-3:', new Date( new Date(0) -3 * 36e5) )  // "1969-12-31T21:00:00.000Z"
console.log( 'Z+8:', new Date(+new Date(0) +8 * 36e5) )  // "1970-01-01T08:00:00.000Z"
&#13;
&#13;
&#13;