网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > 网管学堂 > 网页设计 > Flash > 基础教程 > 网页中时间和日期的各种格式的特效代码  

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

2008-02-28  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

时间和日期在电脑与网络中的显示是多种多样的,电脑开机有时间显示,有些网页中也显示出打开该网页的时间以及显示当前的时间。其实,这做起来很简单,用JavaScript脚本语言就可以很方便地编制出这样的效果来。 网页制作中,时间和日期的各种格式的特效代码如下: 中国网管联盟bitsCN.com


以下是引用片段:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<TITLE>emu's date simple date formatter</TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT="emu"> 
<META NAME="Keywords" CONTENT="date formatter format emu"> 
<META NAME="Description" CONTENT="A function to format Date object into String"> 
</HEAD> 
<BODY> 
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
Date.prototype.format = function (format){ 
 if (format == null) format = "yyyy/MM/dd HH:mm:ss.SSS"; 
 var year = this.getFullYear(); 
 var month = this.getMonth(); 
 var sMonth = ["January","February","March","April","May","June","July","August","September","October","November","December"][month];  网管论坛bbs_bitsCN_com
 var date = this.getDate(); 
 var day = this.getDay(); 
 var hr = this.getHours(); 
 var min = this.getMinutes(); 
 var sec = this.getSeconds(); 
 var daysInYear = Math.ceil((this-new Date(year,0,0))/86400000); 
 var weekInYear = Math.ceil((daysInYear+new Date(year,0,1).getDay())/7); 
 var weekInMonth = Math.ceil((date+new Date(year,month,1).getDay())/7); 
 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)); 

网管u家u.bitscn@com


var d = new Date(); 
alert(d.format());//default format 
alert(d.format("MM-dd-yyyy")); 
alert(d.format("dd/MM/yy")); 
alert(d.format("dd\\MM\\yyyy HH:mm ss.SSS")); 
alert(d.format("yyyy年MM月dd日 HH时mm分ss.SSS秒")); 
alert(d.format("yyyy年MM月dd日 E  HH(0~23)时mm分ss.SSS秒")); 
alert(d.format("yyyy年MM月dd日 E  KK(0~11)时mm分ss.SSS秒 a")); 
alert(d.format("yyyy年MM月dd日 E  hh(1~12)时mm分ss.SSS秒 a")); 
alert(d.format("yyyy年MM月dd日 E  kk(1~24)时mm分ss.SSS秒 今年的第D天,第w周,MMMM这个月的第W周,MMM这个月的第F个E")); 
//--> 
</SCRIPT> 
</BODY> 
</HTML> 中国网管联盟bitsCN.com

以下是纯中文显示的时间与日期:

网管bitscn_com

以下是引用片段:
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
String.prototype.toCHS=function(){return this.replace(/\d/g, function(a){return "零一二三四五六七八九".charAt(parseInt(a))});} 
Number.prototype.toCHS=function(){return((this>19?Math.floor(this/10):"")+(this>9?("十"):"")+(this%10==0&&this>0?"":this%10)).toCHS();} 
Date.prototype.toCHS=function(){ 
with(this)return(getFullYear()+"").toCHS()+"年"+(getMonth()+1).toCHS()+"月"+getDate().toCHS()+"日"+getHours().toCHS()+"时"+getMinutes().toCHS()+"分"+getSeconds().toCHS()+"秒"; 

alert(new Date().toCHS()); 
//--> 
</SCRIPT>

网管bitscn_com


TAGs   特效   代码   格式   各种   时间   日期   网页       <   .replace      
 上一篇:避免在网站设计制作中使用框架页   下一篇:XML和XSLT结合使网站设计浑然一体
网页中时间和日期的各种格式的特效代码 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: