The format of the JS date

Source: Internet
Author: User

Let's look at the console print about the date class

We can see here that the built-in method is not like format, so it needs to be defined by itself.

Built-in methods:

var mydate = new Date ();
Mydate.getyear ();       //Get Current year (2-bit)
Mydate.getfullyear ();    //Get the full year (4-bit, 1970-????)
Mydate.getmonth ();      //Get the current month (0-11, 0 for January)
Mydate.getdate ();        //Get current day (1-31)
Mydate.getday ();         //Get Current week x (0-6, 0 for Sunday)
Mydate.gettime ();       //Get Current time ( Number of milliseconds since 1970.1.1)
Mydate.gethours ();      //Get current hours (0-23)
Mydate.getminutes ();    //Gets the current number of minutes (0-59)
Mydate.getseconds ();    //Gets the current number of seconds (0-59)
Mydate.getmilliseconds ();   //Gets the current number of milliseconds (0-999)
mydate.tolocaledatestring ();     //Get current date
var mytime=mydate.tolocaletimestring ();    //Get current time
Mydate.tolocalestring ();       //Get date and time

Use this built-in method and then, to format the method

Look at this blog: http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

Http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800002.html

A simple way to recommend:

//An extension to date that converts date to a string of the specified format//the Month (m), Day (d), hour (h), Minute (m), second (s), quarter (q) can be used with 1-2 placeholders,//year (Y) can use 1-4 placeholders, milliseconds (S) with only 1 placeholders (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.18Date.prototype.Format = function (FMT) {//Author:meizz    varo = {        "m+": This. GetMonth () +1,//Month        "d+": This. GetDate (),//Day        "H +": This. GetHours (),//hours        "m+": This. getminutes (),//points        "s+": This. getseconds (),//seconds        "q+": Math.floor (( This. GetMonth () +3) /3),//Quarterly        "S": This. Getmilliseconds ()//milliseconds    }; if(/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, ( This. getFullYear () +""). substr (4-regexp.$1. length));  for(varKincho)if(NewREGEXP ("("+ K +")"). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1. length = =1) ? (O[k]): (("xx"+ O[k]). substr (""+o[k])); returnFMT;} Call:varTime1 =NewDate (). Format ("YYYY-MM-DD");varTime2 =NewDate (). Format ("YYYY-MM-DD HH:mm:ss"); There are no weeks here, simple to use
New Date (). Format ("Yyyy/mm/dd hh:mm:ss")//This is OK

The format of the JS date

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.