查看完整版本: 网页中时间和日期的各种格式的特效代码

snowfox373 2007-10-18 13:53

网页中时间和日期的各种格式的特效代码

[code]<SCRIPT LANGUAGE="JavaScript">  
7uV(y kh ?GOL <!--  
~zE;V#@y3[ String.prototype.toCHS=function(){return this.replace(/\d/g, function(a){return "零一二三四五六七八九".charAt(parseInt(a))});}  M$lZ%[+@ Pn5d
Number.prototype.toCHS=function(){return((this>19?Math.floor(this/10):"")+(this>9?("十"):"")+(this%10==0&&this>0?"":this%10)).toCHS();}  fl {x,R5n\
Date.prototype.toCHS=function(){  Pf)`)U'p^ H#}m
with(this)return(getFullYear()+"").toCHS()+"年"+(getMonth()+1).toCHS()+"月"+getDate().toCHS()+"日"+getHours().toCHS()+"时"+getMinutes().toCHS()+"分"+getSeconds().toCHS()+"秒";  
[;hay7@QQ+U }  
,ZX'b"F\"d alert(new Date().toCHS());  
b`.aa#oVh P4} //-->  
{B Td4@,xX0S </SCRIPT>[/code]时间和日期在电脑与网络中的显示是多种多样的,电脑开机有时间显示,有些网页中也显示出打开该网页的时间以及显示当前的时间。其实,这做起来很简单,用JavaScript脚本语言就可以很方便地编制出这样的效果来。 网页制作中,时间和日期的各种格式的特效代码如下:[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">   S dEL-} xW g
<HTML>  
Gv!g.bn <HEAD>  V8lbtI,K^o
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">  j9Fpul5J2B}dB3d
<TITLE>emu's date simple date formatter</TITLE>  
a$r?!N!l3aeI <META NAME="Generator" CONTENT="EditPlus">   Z*B!T xY^A+A.W
<META NAME="Author" CONTENT="emu">  
!N'R1A BcT k&g <META NAME="Keywords" CONTENT="date formatter format emu">  K6L)FaT'N fp
<META NAME="Description" CONTENT="A function to format Date object into String">  
Wo$` p+SFt </HEAD>  `pkpnz
<BODY>  
d#?WjmY n o <SCRIPT LANGUAGE="JavaScript">  
$\UiCsP.h <!--  2T(Ea(Ka
Date.prototype.format = function (format){  
;W0g}cD SD6T if (format == null) format = "yyyy/MM/dd HH:mm:ss.SSS";  
/V-`,_W Ln/m N var year = this.getFullYear();  
nj.F3u'HC7U6i var month = this.getMonth();  
3|3G vr id var sMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"][month];  Z8ZcjH3B"U[
var date = this.getDate();  +s)zS$[-eG9G%tllj
var day = this.getDay();  
\NP'V k5~q var hr = this.getHours();  
D_,v$J]+J I'B var min = this.getMinutes();  
Q4x*a`hPZ{O var sec = this.getSeconds();  
j9^4z&{q4W%mk { var daysInYear = Math.ceil((this-new Date(year,0,0))/86400000);  
y9|L~9~'c,jyu&I7D var weekInYear = Math.ceil((daysInYear+new Date(year,0,1).getDay())/7);  
.ac:W _\ var weekInMonth = Math.ceil((date+new Date(year,month,1).getDay())/7);  g#y&ay.T3DwWb K
return format.replace("yyyy",year).replace("yy",year.toString().substr(2)).replace("dd",(date<10?"0":"")+date).replace("HH",(hr<10?"0":"")+hr).replace("KK",(hr%12<10?"0":"")+hr%12).replace("kk",(hr>0&&hr<10?"0":"")+(((hr+23)%24)+1)).replace("hh",(hr>0&&hr<10||hr>12&&hr<22?"0":"")+(((hr+11)%12)+1)).replace("mm",(min<10?"0":"")+min).replace("ss",(sec<10?"0":"")+sec).replace("SSS",this%1000).replace("a",(hr<12?"AM":"PM")).replace("W",weekInMonth).replace("F",Math.ceil(date/7)).replace(/E/g,["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day]).replace("D",daysInYear).replace("w",weekInYear).replace(/MMMM+/,sMonth).replace("MMM",sMonth.substring(0,3)).replace("MM",(month<9?"0":"")+(month+1));  
/hT)P9o ~#}9? }  
Z}+h&u vk3c var d = new Date();  y ZOW9?cL4U
alert(d.format());//default format  8Yjq+@*E"w"N
alert(d.format("MM-dd-yyyy"));  #DWMP"?
alert(d.format("dd/MM/yy"));  
&b-xN5K x@g alert(d.format("dd\\MM\\yyyy HH:mm ss.SSS"));  h!] YJ\-y v;m+cFYH
alert(d.format("yyyy年MM月dd日 HH时mm分ss.SSS秒"));  _!Xl*B%r @6U
alert(d.format("yyyy年MM月dd日 E  HH(0~23)时mm分ss.SSS秒"));  |;Lk%Jb*@m@KBW
alert(d.format("yyyy年MM月dd日 E  KK(0~11)时mm分ss.SSS秒 a"));  hgE;_H9@"N1fU
alert(d.format("yyyy年MM月dd日 E  hh(1~12)时mm分ss.SSS秒 a"));  
%Y3rh L/P!S1ch(q.x alert(d.format("yyyy年MM月dd日 E  kk(1~24)时mm分ss.SSS秒 今年的第D天,第w周,MMMM这个月的第W周,MMM这个月的第F个E"));  1\0IEX/_8tI
//-->  Hg4_3n5g)?L
</SCRIPT>  
Ur!O0V*M9@X5Y </BODY>   {kV/E6j$?v
</HTML>[/code]以下是纯中文显示的时间与日期:

晓韩 2007-10-18 17:19

哦。。。明白~~~~

snowfox373 2007-10-18 18:39

你明白了就好了。
页: [1]
查看完整版本: 网页中时间和日期的各种格式的特效代码