This article mainly introduces how to implement the FormatDate function output in the time format in javascript, and how to implement the function of inputting the format of the date and time content like the fmt tag. it is a very practical technique, for more information about how to output the FormatDate function in the time format of javascript, see the example in this article. Share it with you for your reference. The details are as follows:
Javascript does not provide a function for inputting datetime content formats like fmt labels:
The following is my time output function, which is directly placed in the tag for calling. The code is as follows:
The code is 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 (), // Hour
"M +": this. getMinutes (), // minute
"S +": this. getSeconds (), // second
"Q +": Math. floor (this. getMonth () + 3)/3), // quarter
"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;
}
Used directly
The code is as follows:
New Date (time variable). Format ("yyyy-MM-dd HH: mm: ss ")
I hope this article will help you design javascript programs.