|
主题:[求助]不同时区打开网页自动显示本时区时间的JS代码 [收藏主题] |
这段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) 顶端 底部 |
<上一主题 | 下一主题 > |