Home > Web Front-end > JS Tutorial > JavaScript implements time format output FormatDate function_javascript skills

JavaScript implements time format output FormatDate function_javascript skills

WBOY
Release: 2016-05-16 16:20:30
Original
1148 people have browsed it

The example in this article describes the implementation of time format output FormatDate function in javascript. Share it with everyone for your reference. The details are as follows:

Javascript does not provide functions for inputting date and time content formats like the fmt tag:
Below is the time output function I downloaded. When using it, put it directly into the label and call it. The code is as follows

Copy code 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(), //hours
"m ": this.getMinutes(), //Minutes
"s": this.getSeconds(), //seconds
" q " : Math.floor((this.getMonth() 3) / 3), //Quarter " 🎜> "S" : this.getMilliseconds()
//milliseconds
};
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 directly when using


Copy code The code is 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 design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template