This article describes the JavaScript implementation of the time format output FormatDate function. Share it for your reference. The details are as follows:
JavaScript does not provide functions that input datetime content formats like the fmt tag:
Below is my time output function, which is directly placed into the tag and called when used. The code is as follows
Copy the code as follows: Date.prototype.Format = function(fmt) { //author: meizz
if (this == "Invalid Date") {
return "";
}
var o = {
"M+" : this.getMonth() + 1, // Month
"d+" : this.getDate(), //Day
"H+" : this.getHours(), //hours
"m+" : this.getMinutes(), //min
"s+" : this.getSeconds(), //sec
"q+" : Math.floor((this.getMonth() + 3) / 3), //Quarterly
"S" : this.getMilliseconds()
//millisecond
};
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;
}
Use it directly when using
Copy the code as follows: new Date(time variable).Format("yyyy-MM-dd HH:mm:ss")
I hope this article will be helpful to everyone's JavaScript programming.