[Example code]:
/** * Formatting of time objects * @param format yyyy-MM-dd HH:mm:ss */ Date.prototype.format = function (format) { if(this=='Invalid Date'){ return ''; } var o = { "M+": this.getMonth() + 1, // month "d+": this.getDate(), // day "h+": this.getHours(), // hour "m+": this.getMinutes(), // minute "s+": this.getSeconds(), // second "q+": Math.floor((this.getMonth() + 3) / 3), // quarter "S": this.getMilliseconds() // millisecond } if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length)); } for (var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; };
[Test code]:
console.info(new Date().format("MM-dd")); console.info(new Date(undefined).format("MM-dd")); console.info(new Date(null).format("MM-dd")); console.info(new Date('').format("MM-dd")); console.info(new Date("").format("MM-dd"));
【Print result】:
05-23 01-01
Donor sharer
I didn't like programming before, but now I am an IT fan obsessed with programming. I would like to share some things I have sorted out and optimized here, hoping to help IT fans, with joy and sweat, and at the same time I also hope that everyone can support it. Of course, if you have money to support a money field (support Alipay and WeChat donations, join the it data center buckle group), but have no money to support a personal field, with your support, we will be more motivated and do better, thank you Ladies and gentlemen.