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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| Date.prototype.format = function(template){ var fullYear = this.getFullYear(); 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(), "w" : "日一二三四五六".charAt(this.getDay()) }
template = template.replace(/y{4}/, fullYear).replace(/y{2}/,fullYear.toString().substring(2));
for(var k in o){ if(o.hasOwnProperty(k)){ var reg = new RegExp(k); template = template.replace(reg, match); } }
function match(m){ console.log(m) return m.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length); } return template; }
var dataTemplateArr = [ 'yy年M月dd日 hh时mm分ss秒', 'yyyy/MM/dd', 'yyyy-MM-dd', 'yy/MM/dd', 'yy-MM-dd', 'yyyy/MM/dd hh:mm:ss', 'yyyy-MM-dd hh:mm:ss', 'yy/MM/dd hh:mm:ss', 'yy-MM-dd hh:mm:ss', 'hh:mm:ss', 'hh:mm' ];
document.write(new Date().format(dataTemplateArr[0]));
|