JavaScript Date format and this month's get

Source: Internet
Author: User
Tags local time

This article on-line collection reprint, feel very useful place, so stay (after all, knowledge by accumulating) ...

1 The primary use of the Date----to take a separate value

Date ()//------Returns the day and time. GetDate ()//---Returns one day in one months from the Date object (1 ~ 31). GetDay ()//----Returns the day of the week from the Date object (0 ~ 6). GetMonth ()//---Returns the month (0 ~ 11) from the Date object. getFullYear ()//returns the year as a four-digit number from a Date object. GetYear ()//----Please use the getFullYear () method instead. GetHours ()//---Returns the hour of the date object (0 ~ 23). Getminutes ()//-Returns the minute of the Date object (0 ~ 59). Getseconds ()//-returns the number of seconds (0 ~ 59) of the Date object. Getmilliseconds ()//returns the milliseconds of the date object (0 ~ 999). GetTime ()//-----Returns the number of milliseconds since January 1, 1970.

2 Primary use----assignment for Date

SetDate ()//-----Set the day of the month in the Date object (1 ~ 31). Setmonth ()//----Set the month (0 ~ 11) in the Date object. setFullYear ()//-sets the year (four digits) in the Date object. Setyear ()//-----Please use the setFullYear () method instead. Sethours ()//----Set the hour (0 ~ 23) in the Date object. Setminutes ()//--sets the minute (0 ~ 59) in the Date object. Setseconds ()//--sets the second (0 ~ 59) in the Date object. Setmilliseconds ()//sets the milliseconds (0 ~ 999) in the Date object. SetTime ()//------Set the Date object in milliseconds.

3 primary use of Date----formatting values

ToString ()//-----Converts the Date object to a string. It doesn't make much sense. totimestring ()//-converts the time portion of a Date object to a string. Standard format, not much meaning todatestring ()//-converts the date part of a Date object to a string. Standard format, not much meaning toutcstring ()//-converts a Date object to a string based on the universal. toLocaleString ()//Converts a Date object to a string based on the local time format. toLocaleTimeString ()//Converts the time portion of a Date object to a string based on the local time format. Of course, it is in the format, but not the custom format tolocaledatestring ()//based on the local time format, convert the date part of the dates object to a string. Of course, in the format, but not the custom format d.valueof ()//----return is the value of the definition, it does not make much sense, if you use the new Date () then return the number of milliseconds, equivalent to gettime ()

4 Date formatting functions, simple formatting

  Extension to date, convert  Date  to string   //  month (m), Day (d), hour (h), Minute (m), seconds (s), quarter (q) in the specified format   can use  1-2  placeholder,   //  year (y) to use  1-4  placeholders, milliseconds (S) can only be used with  1  placeholders (yes  1-3  Digit)    //  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) { //author: meizz      var o = {        "m+"  : this.getmonth () +1,                  //Month         "d+"  : this.getdate (),                     //Day         "H +"  : this.gethours (),                    //hours         "m+"  :  This.getminutes (),                  //points         "s+"  : this.getseconds (),                  //sec          "q+"  : math.floor ((This.getmonth () +3)/3),  //quarterly          "S"   : this.getmilliseconds ()               //Ms      };     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;    } //  Sample Code Var today = new date (). Format (' Yyyy-mm-dd hh:mm:ss. S '); Console.info (today);

5 Date Gets the last day of the first day of the month, the last day of the first day of the week

/** *  first day of the Week  */function showweekfirstday ()  {     var  Nowdate=new date ();      var weekfirstday=new date (Nowdate-( Nowdate.getday ()-1) *86400000);      return weekfirstday.format (' yyyy-MM-dd  Hh:mm:ss. S ')  } /**  *  last day of the Week   */ function showweeklastday ()  {      var nowdate=new date ();      var weekfirstday= New date (nowdate-(Nowdate.getday ()-1) *86400000);      var weeklastday=new  date ((weekfirstday/1000+6*86400) *1000);      return weeklastday.format (' Yyyy-mm-dd hh:mm:ss. S ')  } /**  *  first day of the Month   */ function showmonthfirstday ()  {      var nowdate=new date ();      var monthfirstday= NEw date (Nowdate.getfullyear (), Nowdate.getmonth (), 1);     return  Monthfirstday.format (' Yyyy-m-d hh:mm:ss. S ')  } /**  *  last day of the Month   */ function showmonthlastday ()  {      var nowdate=new date ();     var  Monthnextfirstday=new date (Nowdate.getfullyear (), Nowdate.getmonth () +1,1);      Var monthlastday=new date (MonthNextFirstDay-86400000);     return  Monthlastday.format (' Yyyy-mm-dd hh:mm:ss. S ')  }

Note: The 5th depends on the formatting of the 6th. Well, although these on-line search a lot of, but, oneself summed up again, always feel to learn a lot of things, hehe, knowledge needs to sum up and accumulate ...












JavaScript Date format and this month's get

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.