How to output FormatDate functions in time format using javascript _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.