问题描述
花了我近两个小时的研究,终于算是做出来了。页面代码:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title></title><scriptsrc="/Scripts/jquery-1.4.1.min.js"type="text/javascript"></script><scriptsrc="/Scripts/jQueryCommon.js"type="text/javascript"></script><scripttype="text/javascript">jQuery(function(){vart=setInterval("time()",1000);$("#changeTime").change(function(){time();});$("#format").change(function(){time();});});functiontime(){varhour=parseInt($("#changeTime").val());vardate=newDate();if(hour!=0){vara;if(hour>0){a=newDate().getTime()+(hour*60*60*1000);}else{a=newDate()-(Math.abs(hour)*60*60*1000);}date=newDate(a);}else{date=newDate();}$("#time").text($.convertDate(date,date.format($("#format").val())));}</script></head><body>选择地区:<selectid="changeTime"><optionvalue="0">北京时间</option><optionvalue="-8">伦敦时间</option><optionvalue="-5">开普敦时间</option><optionvalue="+1">东京时间</option></select>选择格式:<selectid="format"><optionvalue="yyyy年MM月dd日hh:mm:ss">yyyy年MM月dd日hh:mm:ss</option><optionvalue="yyyy-MM-ddhh:mm:ss">yyyy-MM-ddhh:mm:ss</option><optionvalue="yyyy/MM/ddhh:mm:ss">yyyy/MM/ddhh:mm:ss</option><optionvalue="MM/dd/yyyyhh:mm:ss">MM/dd/yyyyhh:mm:ss</option><optionvalue="hh:mm:ssyyyy-MM-dd">hh:mm:ssyyyy-MM-dd</option></select><spanid="time"></span></body></html>jQueryCommon.js所要用到的代码:$.extend({"convertDate":function(date,formatter){vards=eval_r("""+date+""");vari=ds.substring(6,19);vardd=newDate(parseInt(i));returndd.format(formatter);}});Date.prototype.format=function(format){varo={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),"S":this.getMilliseconds()};if(/(y+)/.test(format)){format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length));}for(varkino){if(newRegExp("("+k+")").test(format)){format=format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+o[k]).substr((""+o[k]).length));}}returnformat;}
解决方案
解决方案二:
恭喜回复内容太短了!
解决方案三:
欢迎分享