-
Javascript prototype 格式化日期 By shawl.qiu
说明:
继上一次写了一个格式化数字的 prototype 后, 俺一发不可收拾, 一连写了 N 个 prototype.
这个比较有技术含量, 让其现世啰...
上一个公开的 prototype: js prototype 格式化数字 By shawl.qiu
URL: http://blog.csdn.net/btbtd/archive/2006/10/14/1334601.aspx
目录:
1. Javascript Date.prototype 格式化日期 By shawl.qiu 源代码及演示
2. 预览
shawl.qiu
2006-10-21
http://blog.csdn.net/btbtd
1. Javascript Date.prototype 格式化日期 By shawl.qiu 源代码及演示
- linenum
- <%
- Date.prototype.formatDate=function(cdti){
- /*----------------------------------------*/
- * Javascript prototype 格式化日期 By shawl.qiu
- * 2006-10-21
- * http://blog.csdn.net/btbtd
- /*----------------------------------------*/
- var dt=this;
- if(cdti!==undefined){
- return cdti.replace(/(yyyy|nn|dd|hh|mm|ss|ms|wd)/ig,function($1){
- switch($1.toLowerCase()){
- case 'yyyy': return dt.getFullYear();
- case 'nn': return padNum(dt.getMonth()+1, 0, 2);
- case 'dd': return padNum(dt.getDate(), 0, 2);
- case 'hh': return padNum(dt.getHours(), 0, 2);
- case 'mm': return padNum(dt.getMinutes(), 0, 2);
- case 'ss': return padNum(dt.getSeconds(), 0, 2);
- case 'ms': return padNum(dt.getMilliseconds(), 0, 3);
- case 'wd': return week(dt.getDay());
- }
- }
- )
- } else {
- return dt.getFullYear()+'-'+padNum(dt.getMonth()+1, 0, 2)+
- '-'+padNum(dt.getDate(), 0, 2);
- }
- function week(day){
- switch(day){
- case 0:day='日';break;
- case 1:day='一';break;
- case 2:day='二';break;
- case 3:day='三';break;
- case 4:day='四';break;
- case 5:day='五';break;
- case 6:day='六';break;
- }
- return day;
- }
- function padNum(str, num, len){
- var temp=''
- for(var i=0; i<len;temp+=num, i++);
- return temp=(temp+=str).substr(temp.length-len);
- }
- }
- var dt=new Date();
- Response.Write(dt.formatDate()+'<br/>');
- Response.Write(dt.formatDate('yyyy-nn-dd hh:mm:ss ms 周wd '));
- /* 客户端使用
- document.write(dt.formatDate()+'<br/>');
- document.write(dt.formatDate('yyyy-nn-dd hh:mm:ss ms 周wd '));
- */
- %>
2. 预览
-
2006-10-21
2006-10-21 00:06:50 734 周六
Javascript prototype 格式化日期 By shawl.qiu
最新推荐文章于 2024-07-08 16:22:29 发布