1 Date.prototype.format = function(f){ 2 var d = this 3 f = f || "yyyy-MM-dd hh:mm:ss" 4 return f.replace(/[yMdhms]+/g, function(item){ 5 switch (item) { 6 case "yyyy": 7 return d.getFullYear() 8 break 9 case "MM": 10 return +d.getMonth() + 1 < 10 ? "0" + (+d.getMonth() + 1) : +d.getMonth() + 1 11 break 12 case "dd": 13 return +d.getDate() < 10 ? "0" + d.getDate() : d.getDate() 14 break 15 case "hh": 16 return +d.getHours() < 10 ? "0" + d.getHours() : d.getHours() 17 break 18 case "mm": 19 return +d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes() 20 break 21 case "ss": 22 return +d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds() 23 break 24 } 25 }) 26 } 27 28 var d = new Date() 29 console.log(d.format()) 30 console.log(d.format("yyyy/MM/dd"))
时间: 2024-11-08 22:02:03