JavaScript date formatting the use of format functions _javascript tips

Source: Internet
Author: User
Tags getdate

Objective

JavaScript date formatting is common in everyday development, so let's share two examples of how to use the JavaScript time format function.

Method One

Date.prototype.pattern=function (FMT) {var o = {"m+": This.getmonth () +1,//month "d+": this.getdate (),//day "H" + ": this.gethours ()%12 = 0? 12:this.gethours ()%12,//hr "h+": this.gethours (),//hr "m+": this.getminutes (),//min "s+": This.getseconds ()  
 ,//sec "q+": Math.floor (This.getmonth () +3)/3),//Quarter "S": this.getmilliseconds ()//millisecond}; var week = {"0": "/u65e5", "1": "/u4e00", "2": "/u4e8c", "3": "/u4e09", "4": "/u56db", "5": "/u4e  
 "", "6": "/U516D"};  
 if (/(y+)/.test (FMT)) {Fmt=fmt.replace regexp.$1 (this.getfullyear () + ""). substr (4-regexp.$1.length)); } if (/(e+)/.test (FMT)) {fmt=fmt.replace (regexp.$1)? (regexp.$1.length>2?)  
 "/u661f/u671f": "/u5468"): "") +week[this.getday () + ""]); For (var k in O) {if (New RegExp ("+ K +")). Test (FMT)) {FMT = Fmt.replace (regexp.$1, regexp.$1.length==1)? (  
 O[k]): (("+ o[k]"). substr (("" + o[k). length)); }} RetuRN FMT;  }

Call method

var date = new Date (); 
Window.alert (Date.pattern ("Yyyy-mm-dd hh:mm:ss"));

Method Two:

An extension to date that converts date to a string
/month (M) in the specified format, days (d), hours (h), minutes (M), seconds (s), quarter (q) can be used in 1-2 placeholders, 
//year (Y) can be 1-4 placeholders, milliseconds (s) can only be 1 Placeholder (1-3 digits) 
//Example: 
//(New Date ()). Format ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423 
//(New Date ()). Format ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (FMT) { 
 var o = {
 "m+": t His.getmonth () + 1,//month 
 "d+": this.getdate (),//day 
 "h+": this.gethours (),//hr 
 "m+": this.getminutes (),/ /Sub 
 "S+": This.getseconds (),//sec 
 "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]): (("+ o[k]"). substr (("" + o[k). length));
 return FMT;
}

Call Method:

 var haorooms1 = new Date (). Format ("Yyyy-mm-dd");

 var haorooms2= new Date (). Format ("Yyyy-mm-dd hh:mm:ss");

 alert (HAOROOMS1); alert (HAOROOMS2);

Summarize

The above is the whole story of this article, the above two methods from the network, verified testing are available, but also more convenient, everyone in use, you can package it, in your function, direct call on it, these two time format function method is better! I hope to help you with your study or work. If you have questions, you can exchange messages.

Related Article

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.