1 <span id="time"></span> 2 <script> 3 4 //名称:日期加法函数 5 //参数:part(year、month、day、hour、minute、second、millisecond) 6 //返回:Date对象 7 Date.prototype.add = function (part, value) { 8 if (!value || isNaN(value)) value = 0; 9 switch (part) { 10 case "year": 11 this.setFullYear(this.getFullYear() + value); 12 break; 13 case "month": 14 this.setMonth(this.getMonth() + value); 15 break; 16 case "day": 17 this.setDate(this.getDate() + value); 18 break; 19 case "hour": 20 this.setHours(this.getHours() + value); 21 break; 22 case "minute": 23 this.setMinutes(this.getMinutes() + value); 24 break; 25 case "second": 26 this.setSeconds(this.getSeconds() + value); 27 break; 28 case "millisecond": 29 this.setMilliseconds(this.getMilliseconds() + value); 30 break; 31 default: 32 } 33 return this; 34 }; 35 36 Date.prototype.addYears = function (value) { 37 if (!value || isNaN(value)) value = 0; 38 this.setFullYear(this.getFullYear() + value); 39 return this; 40 }; 41 42 Date.prototype.addMonths = function (value) { 43 if (!value || isNaN(value)) value = 0; 44 this.setMonth(this.getMonth() + value); 45 return this; 46 }; 47 48 Date.prototype.addDays = function (value) { 49 if (!value || isNaN(value)) value = 0; 50 this.setDate(this.getDate() + value); 51 return this; 52 }; 53 54 Date.prototype.addHours = function (value) { 55 if (!value || isNaN(value)) value = 0; 56 this.setHours(this.getHours() + value); 57 return this; 58 }; 59 60 Date.prototype.addMinutes = function (value) { 61 if (!value || isNaN(value)) value = 0; 62 this.setMinutes(this.getMinutes() + value); 63 return this; 64 }; 65 66 Date.prototype.addSeconds = function (value) { 67 if (!value || isNaN(value)) value = 0; 68 this.setSeconds(this.getSeconds() + value); 69 return this; 70 }; 71 72 Date.prototype.addMilliseconds = function (value) { 73 if (!value || isNaN(value)) value = 0; 74 this.setMilliseconds(this.getMilliseconds() + value); 75 return this; 76 }; 77 78 //名称:日期加法函数 79 //参数:time(日期字符串,示例:12:00:00) 80 //返回:Date对象 81 Date.prototype.addTime = function (time) { 82 var timeRegex = /^([0-1]?d|2[0-3])(:[0-5]?d){1,2}$/g; 83 if (timeRegex.test(time)) { 84 var value = Date.parse("1970/1/1 " + time) - Date.parse("1970/1/1"); 85 this.setMilliseconds(this.getMilliseconds() + value); 86 } 87 return this; 88 }; 89 90 //名称:日期格式化函数 91 //参数:format(示例:yyyy-MM-dd hh:mm:ss)、zeroize(是否补零) 92 //返回:日期字符串 93 Date.prototype.toCustomString = function (format, zeroize) { 94 if (!zeroize) zeroize = false; 95 var dy = this.getFullYear(); 96 var dM = this.getMonth() + 1; 97 var dd = this.getDate(); 98 var dh = this.getHours(); 99 var dm = this.getMinutes(); 100 var ds = this.getSeconds(); 101 var dS = this.getMilliseconds(); 102 var orm = { 103 "y+": dy.toString(), 104 "M+": !zeroize ? dM.toString() : dM < 10 ? '0' + dM : dM.toString(), 105 "d+": !zeroize ? dd.toString() : dd < 10 ? '0' + dd : dd.toString(), 106 "h+": !zeroize ? dh.toString() : dh < 10 ? '0' + dh : dh.toString(), 107 "m+": !zeroize ? dm.toString() : dm < 10 ? '0' + dm : dm.toString(), 108 "s+": !zeroize ? ds.toString() : ds < 10 ? '0' + ds : ds.toString(), 109 "S": dS.toString() 110 }; 111 for (var i in orm) { 112 var patt = new RegExp(i); 113 if (patt.test(format)) { 114 var item = orm[i]; 115 var ms = format.match(patt); 116 var result = ms[0]; 117 if (i === "S") { 118 format = format.replace(result, item); 119 } else { 120 format = format.replace(result, item.substr(item.length - result.length)); 121 } 122 } 123 } 124 return format; 125 }; 126 window.onload = function(){ 127 var time = document.getElementById("time"); 128 129 setInterval('time.innerText = new Date().toCustomString("yyyy-MM-dd hh:mm:ss")',1000); 130 131 } 132 133 </script>
没有格式啥的要求的话,就用Date下的toLocaleString()显示年月日时间或者toLocaleDateString()显示年月日
1 <span id="time"></span> 2 <script> 3 var time = document.getElementById("time"); 4 setInterval('time.innerHTML = new Date().toLocaleString()', 1000); 5 </script>