首页 > 代码库 > 时间日期格式化成需要的形式

时间日期格式化成需要的形式

技术分享

--------------------------------------------------------------------------------------------------------------------------------

 1 <!doctype html> 2 <html lang="en"> 3  <head> 4   <meta charset="UTF-8"> 5   <meta name="Generator" content="EditPlus®"> 6   <meta name="Author" content=""> 7   <meta name="Keywords" content=""> 8   <meta name="Description" content=""> 9   <script src="http://192.168.1.202/1_exer/0_resources/jq/jquery-1.11.3.js"></script>10   <title>时间日期相关</title>11  </head>12  <body>13   <script>14     /*  时间对象的格式化 */15     Date.prototype.format = function(format){16         var o = {17             "M+" : this.getMonth() + 1,18             "d+" : this.getDate(),19             "h+" : this.getHours(),20             "m+" : this.getMinutes(),21             "s+" : this.getSeconds(),22             "q+" : Math.floor((this.getMonth() + 3) / 3),23             "S" : this.getMilliseconds()24         }25         if (/(y+)/.test(format)){26             format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(427                     - RegExp.$1.length)); }28         for (var k in o){29             if (new RegExp("(" + k + ")").test(format)){30                 format = format.replace(RegExp.$1, RegExp.$1.length == 1? o[k]: ("00" + o[k]).substr(("" + o[k]).length));}  }31         return format;32     }33     //以下为调用34     $(function(){35         var date=new Date();36         var dateFormat=date.format(yyyy-MM-dd hh:mm:ss);37         var dateFormat3=date.format(yyyy/MM/dd hh:mm:ss);38         $(.dateTime1).html(date);39         $(.dateTime2).html(dateFormat);40         $(.dateTime3).html(dateFormat3);41         console.log(234);42     });43   </script>44   <p>格式化前:<span class="dateTime1"></span></p>45   <p>格式化yyyy-MM-dd hh:mm:ss后:<span class="dateTime2"></span></p>46   <p>格式化yyyy/MM/dd hh:mm:ss后:<span class="dateTime3"></span></p>47  </body>48 </html>

 

时间日期格式化成需要的形式