JS日期格式化(网上转载)

China☆狼群 提交于 2019-11-29 15:57:57

JS日期格式化(网上转载)

 

 

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3     <head>
 4         <base href="<%=basePath%>">
 5 
 6         <title>My JSP 'index.jsp' starting page</title>
 7         <meta http-equiv="pragma" content="no-cache">
 8         <meta http-equiv="cache-control" content="no-cache">
 9         <meta http-equiv="expires" content="0">
10         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
11         <meta http-equiv="description" content="This is my page">
12         <!--
13     <link rel="stylesheet" type="text/css" href="styles.css">
14     -->
15         <script type="text/javascript">
16 window.onload = function() {
17     document.getElementById("textContext").value = new Date()
18             .format("yyyy-MM-dd");
19     ;
20 }
21 
22 /**
23  * 时间对象的格式化
24  */
25 Date.prototype.format = function(format) {
26     /*
27      * format="yyyy-MM-dd hh:mm:ss";
28      */
29     var o = {
30         "M+" : this.getMonth() + 1,
31         "d+" : this.getDate(),
32         "h+" : this.getHours(),
33         "m+" : this.getMinutes(),
34         "s+" : this.getSeconds(),
35         "q+" : Math.floor((this.getMonth() + 3) / 3),
36         "S" : this.getMilliseconds()
37     }
38 
39     if (/(y+)/.test(format)) {
40         format = format.replace(RegExp.$1, (this.getFullYear() + "")
41                 .substr(4 - RegExp.$1.length));
42     }
43 
44     for ( var k in o) {
45         if (new RegExp("(" + k + ")").test(format)) {
46             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
47                     : ("00" + o[k]).substr(("" + o[k]).length));
48         }
49     }
50     return format;
51 }
52 
53 /**
54  * 时间对象的格式化
55  */
56 Date.prototype.format = function(format) {
57     /*
58      * format="yyyy-MM-dd hh:mm:ss";
59      */
60     var o = {
61         "M+" : this.getMonth() + 1,
62         "d+" : this.getDate(),
63         "h+" : this.getHours(),
64         "m+" : this.getMinutes(),
65         "s+" : this.getSeconds(),
66         "q+" : Math.floor((this.getMonth() + 3) / 3),
67         "S" : this.getMilliseconds()
68     }
69 
70     if (/(y+)/.test(format)) {
71         format = format.replace(RegExp.$1, (this.getFullYear() + "")
72                 .substr(4 - RegExp.$1.length));
73     }
74 
75     for ( var k in o) {
76         if (new RegExp("(" + k + ")").test(format)) {
77             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
78                     : ("00" + o[k]).substr(("" + o[k]).length));
79         }
80     }
81     return format;
82 }
83 </script>
84     </head>
85 
86     <body>
87         <input id="textContext">
88         <input type="button" onclick="">
89     </body>
90 </html>

来自:http://user.qzone.qq.com/526605799/2

转载于:https://my.oschina.net/u/1590001/blog/268206

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!