js的日期格式化函数

//-----------------------------------------------------------------------------------------------------

js的日期格式化函数

<script type="text/javascript">
Date.prototype.format = function(format)powered by 25175.net
{
    var o =
    {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format))
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
    if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}

var ddd = new Date();
document.write (ddd.format('yy-MM-dd hh:mm:ss'));
</script>

//2------------------------------------------------------------------------------------------------------------------------------------------------

js 日期格式化

  1. /*   
  2. 函数:格式化日期   
  3. 参数:formatStr-格式化字符串   
  4. d:将日显示为不带前导零的数字,如1   
  5. dd:将日显示为带前导零的数字,如01   
  6. ddd:将日显示为缩写形式,如Sun   
  7. dddd:将日显示为全名,如Sunday   
  8. M:将月份显示为不带前导零的数字,如一月显示为1   
  9. MM:将月份显示为带前导零的数字,如01  
  10. MMM:将月份显示为缩写形式,如Jan  
  11. MMMM:将月份显示为完整月份名,如January  
  12. yy:以两位数字格式显示年份  
  13. yyyy:以四位数字格式显示年份  
  14. h:使用12小时制将小时显示为不带前导零的数字,注意||的用法  
  15. hh:使用12小时制将小时显示为带前导零的数字  
  16. H:使用24小时制将小时显示为不带前导零的数字  
  17. HH:使用24小时制将小时显示为带前导零的数字  
  18. m:将分钟显示为不带前导零的数字  
  19. mm:将分钟显示为带前导零的数字  
  20. s:将秒显示为不带前导零的数字  
  21. ss:将秒显示为带前导零的数字  
  22. l:将毫秒显示为不带前导零的数字  
  23. ll:将毫秒显示为带前导零的数字  
  24. tt:显示am/pm  
  25. TT:显示AM/PM  
  26. 返回:格式化后的日期  
  27. */ 
  28. Date.prototype.format = function (formatStr) {  
  29. var date = this;  
  30. /*  
  31. 函数:填充0字符  
  32. 参数:value-需要填充的字符串, length-总长度  
  33. 返回:填充后的字符串  
  34. */ 
  35. var zeroize = function (value, length) {  
  36. if (!length) {  
  37. length = 2;  
  38. }  
  39. value = new String(value);  
  40. for (var i = 0, zeros = ''; i < (length - value.length); i++) {  
  41. zeros += '0';  
  42. }  
  43. return zeros + value;  
  44. };  
  45. return formatStr.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|M{1,4}|yy(?:yy)?|([hHmstT])\1?|[lLZ])\b/g, function($0) {  
  46. switch ($0) {  
  47. case 'd'return date.getDate();  
  48. case 'dd'return zeroize(date.getDate());  
  49. case 'ddd'return ['Sun''Mon''Tue''Wed''Thr''Fri''Sat'][date.getDay()];  
  50. case 'dddd'return ['Sunday''Monday''Tuesday''Wednesday''Thursday''Friday''Saturday'][date.getDay()];  
  51. case 'M'return date.getMonth() + 1;  
  52. case 'MM'return zeroize(date.getMonth() + 1);  
  53. case 'MMM'return ['Jan''Feb''Mar''Apr''May''Jun''Jul''Aug''Sep''Oct''Nov''Dec'][date.getMonth()];  
  54. case 'MMMM'return ['January''February''March''April''May''June''July''August''September''October''November''December'][date.getMonth()];  
  55. case 'yy'return new String(date.getFullYear()).substr(2);  
  56. case 'yyyy'return date.getFullYear();  
  57. case 'h'return date.getHours() % 12 || 12;  
  58. case 'hh'return zeroize(date.getHours() % 12 || 12);  
  59. case 'H'return date.getHours();  
  60. case 'HH'return zeroize(date.getHours());  
  61. case 'm'return date.getMinutes();  
  62. case 'mm'return zeroize(date.getMinutes());  
  63. case 's'return date.getSeconds();  
  64. case 'ss'return zeroize(date.getSeconds());  
  65. case 'l'return date.getMilliseconds();  
  66. case 'll'return zeroize(date.getMilliseconds());  
  67. case 'tt'return date.getHours() < 12 ? 'am' : 'pm';  
  68. case 'TT'return date.getHours() < 12 ? 'AM' : 'PM';  
  69. }  
  70. });  

测试:

  1. <script type="text/javascript">  
  2. document.writeln(new Date().format("yyyy-MM-dd hh:mm:ss"));  
  3. </script> 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值