账号通
    

账号  

密码  

1700

查看

0

回复
主题:[求助]不同时区打开网页自动显示本时区时间的JS代码 [收藏主题]  
zjsoccer 当前离线

67

主题

0

广播

0

粉丝
添加关注
级别:学前班

用户积分:263 分
登录次数:54 次
注册时间:2009/12/13
最后登录:2015/12/6
zjsoccer 发表于:2013/4/18 11:53:57   | 只看该作者 查看该作者主题 楼主 
科汛智能建站系统
这段JS代码是控制足球比赛日期和时间的,也就是网页上的足球比赛时间是随着在不同时区打开网页时间只显示本时区时间。这段代码是以0时区为 准的,我想把他修改成以东8区,也就是北京时间为准,改哪一个可以改成我想要的,谢谢!


var TZAdjustTimeZone = function(time_class, date_class, already_adjusted_flag_class){
  this.getCorrection = function(){
    var tz_offset = new Date().getTimezoneOffset()/60;
    return 0 - tz_offset;
  }


  this.getTimeObjects = function(){
    return jQuery('.'+this.time_class+':not(.'+this.already_adjusted_flag_class+')');
  }


  this.zerofill = function(num, zeros){
    str = num.toString();
    while(str.length < zeros){
      str = "0" + str;
    }
    return str;
  }


  this.adjust = function(time_object, correct){
    var to = jQuery(time_object);
    var string_hora = to.html();
    var regex_hora = /([^:]|^)([0-1][0-9]|2[0-3])(:[0-5][0-9]([:][0-5][0-9])?)/
    var r = regex_hora.exec(string_hora);
    //console.log(typeof r);
    if(r != null){
      var ajustada = (correct+parseInt(r[2], 10));
      if(ajustada > 23){
        ajustada = ajustada - 24;
        this.adjustDate(to, 1);
      } else if(ajustada < 0){
        ajustada = ajustada + 24;
        this.adjustDate(to, -1);
      }
      to.html(string_hora.replace(r[0], r[1] + this.zerofill(ajustada, 2) + r[3]));
      to.addClass(this.already_adjusted_flag_class);
    }


  }


  this.adjustDate = function(to, correct){
    var dob = to.parent().find('.' + this.date_class + ':not(.'+this.already_adjusted_flag_class+')'); //date object
    var string_fecha = dob.html();
    var regex_fecha = /([0-3][0-9]).([01][0-9]).([0-9][0-9][0-9][0-9])/
    var r = regex_fecha.exec(string_fecha);
    var d = new Date(parseInt(r[3],10), r[2]-1, parseInt(r[1],10) + correct);
    var corrected = this.zerofill(d.getDate(),2) + "." + this.zerofill(d.getMonth()+1,2) + "." + (d.getFullYear());
    dob.html(string_fecha.replace(r[0], corrected));
    dob.addClass(this.already_adjusted_flag_class);
  }


  this.adjustAll = function(){
    var tz_correct = this.getCorrection();
    var tz_time_objects = this.getTimeObjects();
    var that = this;
    tz_time_objects.each(function(){
      that.adjust(this, tz_correct);
    });
    window.adjust_tz = true;
    if(typeof console == "object" && typeof console.log == "function"){
      console.log("Done adjusting timezone.  Correction: "+tz_correct);
    }
  }


  this.setClasses = function(time_class, date_class, already_adjusted_flag_class){
    this.time_class = time_class;
    this.date_class = date_class;
    this.already_adjusted_flag_class = already_adjusted_flag_class;
  }


  // Inicializacion
  this.setClasses(time_class, date_class, already_adjusted_flag_class);
}


if(typeof jQuery == 'undefined') {alert("Se necesita jQuery para corregir la hora.");}


jQuery(function(jQuery){
  var DFAdjustTimeZone = new TZAdjustTimeZone("hora","dia","tz_ajustada");
  DFAdjustTimeZone.adjustAll();
});
 
  支持(0) | 反对(0) 回到顶部顶端 回到底部底部
<上一主题 | 下一主题 >
Powered By KesionCMS Version X1
厦门科汛软件有限公司 © 2006-2016 页面执行0.04688秒 powered by KesionCMS 9.0