1. 程式人生 > >JavaScript格式化日期輸出

JavaScript格式化日期輸出

javascrip img tab solid round -m min time lac

JavaScript Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<script>
window.onload = function(){
Date.
prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+"
: this.getDate(), //日
"H+": this.getHours(), //小時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() +
3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (
this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length ==
1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

var currenttime1 = new Date().Format("yyyy-MM-ddTHH:mm:ss");
var pselstart = document.getElementById("starttime");
pselstart.value = currenttime1;
var currenttime2 = new Date().Format("yyyy-MM-ddTHH:mm:ss");
var pselend = document.getElementById("endtime");
pselend.value = currenttime2;
}
</script>

技術分享

JavaScript格式化日期輸出