A-A+

js格式化时间日期程序代码

2016年10月16日 前端设计 暂无评论 阅读 6 views 次

本文章给大家总结一些常见的格式化时间日期程序方法,有需要了解的朋友可进入参考。

例1,代码如下:

  1. /** 
  2.  * 时间对象的格式化; 
  3.  */  
  4. Date.prototype.format = function(format) {  
  5.     /* 
  6.      * eg:format="YYYY-MM-dd hh:mm:ss"; 
  7.      */  
  8.     var o = {  
  9.         "M+" :this.getMonth() + 1, // month  
  10.         "d+" :this.getDate(), // day  
  11.         "h+" :this.getHours(), // hour  
  12.         "m+" :this.getMinutes(), // minute  
  13.         "s+" :this.getSeconds(), // second  
  14.         "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter  
  15.         "S" :this.getMilliseconds()  
  16.     // millisecond  
  17.     }//xiariboke.net  
  18.     if (/(y+)/.test(format)) {  
  19.         format = format.replace(RegExp.$1, (this.getFullYear() + "")  
  20.                 .substr(4 - RegExp.$1.length));  
  21.     }  
  22.     for ( var k in o) {  
  23.         if (new RegExp("(" + k + ")").test(format)) {  
  24.             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]  
  25.                     : ("00" + o[k]).substr(("" + o[k]).length));  
  26.         }  
  27.     }  
  28.     return format;  
  29. }  

调用方法,代码如下:

var now = new Date().format("yyyy-MM-dd hh:mm:ss");

例2,js格式化当前时间为yyyy-mm-dd形式,代码如下:

  1. function getNowFormatDate()   
  2. {   
  3. var day = new Date();   
  4. var Year = 0;   
  5. var Month = 0;   
  6. var Day = 0;   
  7. var CurrentDate = "";   
  8. //初始化时间   
  9. //Year= day.getYear();//有火狐下2008年显示108的bug   
  10. Year= day.getFullYear();//ie火狐下都可以   
  11. Month= day.getMonth()+1;   
  12. Day = day.getDate();   
  13. //Hour = day.getHours();   
  14. // Minute = day.getMinutes();   
  15. // Second = day.getSeconds();   
  16. CurrentDate += Year + "-";   
  17. if (Month >= 10 )   
  18. {   
  19. CurrentDate += Month + "-";   
  20. }   
  21. else   
  22. {   
  23. CurrentDate += "0" + Month + "-";   
  24. }   
  25. if (Day >= 10 )   
  26. {   
  27. CurrentDate += Day ;   
  28. }   
  29. else   
  30. {   
  31. CurrentDate += "0" + Day ;   
  32. }   
  33. return CurrentDate;   
  34. }  

例3,代码如下:

  1. Date.prototype.format = function(format){   
  2. var o = {   
  3. "M+" : this.getMonth()+1, //month   
  4. "d+" : this.getDate(), //day   
  5. "h+" : this.getHours(), //hour   
  6. "m+" : this.getMinutes(), //minute   
  7. "s+" : this.getSeconds(), //second   
  8. "q+" : Math.floor((this.getMonth()+3)/3), //quarter   
  9. "S" : this.getMilliseconds() //millisecond   
  10. }  
  11. if(/(y+)/.test(format)) {   
  12. format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  13. }  
  14. for(var k in o) {   
  15. if(new RegExp("("+ k +")").test(format)) {   
  16. format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));   
  17. }   
  18. }   
  19. return format;   
  20. }  
  21. //使用方法   
  22. var now = new Date();   
  23. var nowStr = now.format("yyyy-MM-dd hh:mm:ss");   
  24. //使用方法2:   
  25. var testDate = new Date();   
  26. var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");   
  27. alert(testStr);   
  28. //示例:   
  29. alert(new Date().Format("yyyy年MM月dd日"));   
  30. alert(new Date().Format("MM/dd/yyyy"));   
  31. alert(new Date().Format("yyyyMMdd"));   
  32. alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));  
标签:

给我留言