Date object of JavaScript

Source: Internet
Author: User

I. Basic Knowledge

1. Date object introduction:

1 // Date object: used to process the Date and time. 2/* Syntax: 3 var date_obj = new Date (arg); 4 arg: an optional parameter of the Date constructor. If the Date object is omitted, it automatically saves the current Date and time as the initial value. Note: The Date and time obtained by the Data object is based on the user client (it is not always reliable ), to get the server's time and Date value, see PHP Date and time 6 arg: parameter description: 7 */8 // --------------------- simple instance 1 ----------------------- 9 var d1 = new Date (); 10 document. write (d1 + "<br/>"); // output: Thu Jan 17 2013 10:46:50 GMT + 0800 (China Standard Time) 11 // or simpler: document. write (Date (); 12 13 // ------------------ the Date object is formatted as the local time -------- 14 document. write (d1.toLocaleDateString (); // output: February 15, January 17, 2013 </script>

2. getFullYear: the year in which four digits are obtained.

1 <script language = "javascript"> 2 // getFullYear: Get the year of 4 digits 3/* Syntax: 4 var Mydate = new Date (); 5 Mydate. getFullYear (); 6 prompt: This method always combines a Date object to use 7 */8 // ------------------------ get the year ------------------- 9 var d1 = new Date () from the current time (); 10 document. write (d1.getFullYear () + "<br/>"); // output 201311 // ------------------------ obtain the year from the specific Date ----------------- 12 var d2 = new Date ); // The following parameters: yaer, month, date, [hour, minute, second] 13 document. write (d2.getFullYear (); // output 200814 // prompt: Date. the getYear method has been discarded and replaced by getFullYear 15 </script>

3. getMonth: Get the number representing the month

1 <script language = "javascript"> 2 // getMonth: Get the number indicating the month. 3/* Syntax: 4 var Mydate = new Date (); 5 Mydate. getMonth (); 6 prompt: This method always uses a Date object to get [0-11] digits in 7 to represent the month of [1-12], that is: number of returned results + 1 = Month 8 */9 // ------------------------ obtain the month from the current time ----------------- 10 var d = new Date (); 11 document. write (d. getMonth () + "<br/>"); // output 0, indicating January 12 // ------------------------ obtain the year from the specific Date ----------------- 13 var d2 = new Date (); 14 month_array = new Array ('1', '2', '3', '4', '5', '6', '7', '8 ', '9', '10', '11', '12'); 15 document. write ("now" + month_array [d2.getMonth ()] + "month"); // This is more perfect, 16 more simple than switch </script>

4. getDate: Get a day that represents the month

1 <script language = "javascript"> 2 // getDate: obtain a day indicating the month. The syntax is as follows: 4 var Mydate = new Date (); 5 Mydate. getDate (); 6 prompt: this method is always combined with a Date object to use 7 to get the number 1-31 to indicate the day of the month 8 */9 // ---------------------- from the current month which day is obtained ----------------- 10 var d = new Date (); 11 document. write (d. getDate () + "<br/>"); // output: 1712 </script>

5. getDay: Get a day of the week

1 <script language = "javascript"> 2 // getDay: Get a day of the week. 3/* Syntax: 4 var Mydate = new Date (); 5 Mydate. getDay (); 6 prompt: This method always combines a Date object to use 7 to get the number [0-6] to represent the day of [from Sunday to Saturday], 0 to Sunday, 1 indicates Monday 8 */9 // ------------------------ get --------------------- 10 var d = new Date (); 11 document. write (d. getDay () + "<br/>"); // output 4, corresponding to Thursday 12 13 // ------------------------ output ------------------- 14 var d2 = new Date (); 15 var week_array = new Array ('day', '1 ', '2', '3', '4', '5', '6'); 16 document. write ("Week" + week_array [d2.getDay ()]); // output Thursday 17 // we found this method is very brief, than if .. else and switch are more convenient than 18 </script>

6. hour, minute, second, and millisecond operations:

1 <script language = "javascript"> 2 // hour, minute, second, millisecond 3 //****************** * ******************* getHours ****************** * ******************* 4 // getHours: this method is used to obtain the hour field in the Date object ----- [note writing: getHours instead of "getHour"] 5/* Syntax: 6 var Mydate = new Date (); 7 Mydate. getHours (); 8 prompt: This method always combines a Date object to use 9 to get an integer ranging from 0 to 23. 10 */11 // ------------------------ get ----------------------- 12 var d = new Date (); 13 document. write (d. getHours () + "<br/>"); // 1114 15 // -------------------------- friendly display --------------------------- 16 // when the hour number is less than 10, the result is a one-digit number instead of two digits (for example, get 5 instead of 05). You can use the function to implement 17 function checkTime (I) {18 if (I <10) {I = "0" + I; return I;} 19 else {return I;} 20} 21 document. write (checkTime (d. getHours () + "<br/>"); 22 23 //******************* * ****************** GetMinutes ******************* * ****************** 24 // getMinutes: this method is used to obtain the minute field in the Date object ----- [note writing: getMinutes instead of "getMinute"] 25/* Syntax: 26 var Mydate = new Date (); 27 Mydate. getMinutes (); 28 prompt: This method always uses a Date object to get a 0-59 integer in 29, indicating 0 to 59 points in an hour. 30 */31 // ------------------------ get ----------------------- 32 var d = new Date (); 33 document. write (d. getMinutes () + "<br/> "); 34 // other friendly operations are the same as getHours35 36 //**************************** * ********** getSeconds **************************** * ********* 37 // Similarly, getSeconds: obtain the number of seconds field, usage is the same as above 38 //********************************** * ***** getMilliseconds ******************************** 40/ /Similarly, getMilliseconds: returns the number in milliseconds. Same as above 41 // prompt: Get an integer of 0-999 to indicate 0 to 999 milliseconds in one second. 42 document. write (d. getMilliseconds () + "<br/>"); // 55343 44 </script>

7. getTime: Get the timestamp (the number of milliseconds between the Date object and on January 1, January 1, 1970 (GMT)

1 <script language = "javascript"> 2 // getTime: Obtain the timestamp (the number of milliseconds between the Date object and GMT on January 1, January 1, 1970) 3 var d = new Date (); 4 document. write (d. getTime (); // 13583945181025 </script>

 

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.