Javascript prototype 格式化日期 By shawl.qiu

78 篇文章 0 订阅
31 篇文章 0 订阅

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 源代码及演示
  1. linenum
  2. <%
  3.     Date.prototype.formatDate=function(cdti){
  4.     /*----------------------------------------*/
  5.      * Javascript prototype 格式化日期 By shawl.qiu
  6.      * 2006-10-21
  7.      *   http://blog.csdn.net/btbtd  
  8.     /*----------------------------------------*/
  9.         var dt=this;    
  10.         if(cdti!==undefined){
  11.             return cdti.replace(/(yyyy|nn|dd|hh|mm|ss|ms|wd)/ig,function($1){
  12.                     switch($1.toLowerCase()){
  13.                         case 'yyyy': return dt.getFullYear();
  14.                         case 'nn': return padNum(dt.getMonth()+1, 0, 2);
  15.                         case 'dd': return padNum(dt.getDate(), 0, 2);
  16.                         case 'hh': return padNum(dt.getHours(), 0, 2);
  17.                         case 'mm': return padNum(dt.getMinutes(), 0, 2);
  18.                         case 'ss': return padNum(dt.getSeconds(), 0, 2);
  19.                         case 'ms': return padNum(dt.getMilliseconds(), 0, 3);
  20.                         case 'wd': return week(dt.getDay());
  21.                     }
  22.                 }
  23.             )
  24.         } else {
  25.             return dt.getFullYear()+'-'+padNum(dt.getMonth()+1, 0, 2)+
  26.             '-'+padNum(dt.getDate(), 0, 2);
  27.         }
  28.         function week(day){
  29.             switch(day){
  30.                 case 0:day='日';break;
  31.                 case 1:day='一';break;
  32.                 case 2:day='二';break;
  33.                 case 3:day='三';break;
  34.                 case 4:day='四';break;
  35.                 case 5:day='五';break;
  36.                 case 6:day='六';break;
  37.             }
  38.             return day;
  39.         }
  40.         function padNum(str, num, len){
  41.             var temp=''
  42.             for(var i=0; i<len;temp+=num, i++);
  43.             return temp=(temp+=str).substr(temp.length-len);
  44.         }
  45.     }
  46.     var dt=new Date();
  47.         Response.Write(dt.formatDate()+'<br/>');
  48.         Response.Write(dt.formatDate('yyyy-nn-dd hh:mm:ss ms 周wd '));
  49. /*         客户端使用 
  50.         document.write(dt.formatDate()+'<br/>');
  51.         document.write(dt.formatDate('yyyy-nn-dd hh:mm:ss ms 周wd '));
  52.          */
  53. %>

2. 预览
2006-10-21
2006-10-21 00:06:50 734 周六


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值