JS/JQ Basics (daily housekeeping Records)-Pure JS format time

Source: Internet
Author: User

First, Pure JS format time

 Previously recorded some, work found in the more commonly used, just record it.

Because of the very foundation, it is directly posted out, do not do analysis.

Transform the prototype of date

Date.prototype.format =function(format) {varn \ {     "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(varKincho) {if(NewRegExp ("(" + K + ")"). Test (format)) {format= Format.replace (regexp.$1, Regexp.$1.length==1 o[k]: ("XX" + o[k]). substr ("" +O[k]) (length)); }     }     returnformat;} 

Let's see how to use it.

//How to usevarnow =NewDate ();varNowstr = Now.format ("Yyyy-mm-dd hh:mm:ss"); //Use Method 2:varTestDate =NewDate ();varTeststr = Testdate.format ("yyyy mm month DD day hh hours mm min ss sec"); alert (TESTSTR); //Example:Console.log (NewDate (). Format ("yyyy mm month DD Day")); Console.log (NewDate (). Format ("mm/dd/yyyy")); Console.log (NewDate (). Format ("YyyyMMdd")); Console.log (NewDate (). Format ("Yyyy-mm-dd hh:mm:ss"));

Look at the results of the implementation.

Still good use of it, put this short JS code into the common JS file, and then directly use it, you do not have to transform the ha.

JS/JQ Basics (daily housekeeping Records)-Pure JS format time

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.