Commonly used built-in objects (Date, Math)

Date

  Date main role is to deal with the time and date.

Date Format

1 var today = new Date()
2 var d1 = new Date("October 13, 1999 11:13:00")
3 var d2 = new Date(79,5,24)
4 var d3 = new Date(79,5,24,11,33,0)

Date object methods (commonly used)

. 1  getDate ()    
 2  // Returns the Date object of the week in a day (~ 0. 6) 
. 3 getDay ()        // weeks starting from 0 
4  // four-digit year return Date object 
. 5  the getFullYear ()    
 6  // Date object h (~ 23 is 0) 
. 7  getHours ()
 . 8  // Date object millisecond value (0 ~ 999)     
. 9  getMilliseconds ()
 10  // Date object minutes (0 ~ 59)     
. 11  getMinutes ()    
 12  // Date object month (-. 11. 1) 
13 is the getMonth () +. 1       // is zero, the real need for the month plus 1 
14  // Date object sec (0 to 59) 
15 the getSeconds ()    
 16  // returns the number of milliseconds January 1, 1970 to date 
. 17  the getTime ()    
 18 is  the setFullYear ()    
 . 19  // Date object of the month (1 to 31) one day 
20 is  setDate ()    
 21 is  // Set Date object h (~ 23 is. 1) 
22 is  setHours ()    
 23 is  // Date object in minutes (0 ~ 59) 
24  the setMinutes ()
 25  // set seconds (0 ~ 59) Date object     
26 is  the setSeconds ()    
 27  // Date object in milliseconds (0 ~ 999) 
28  setMilliScondes ()    
 29  // returned Date object month from one day (1 to 31) 
30  // the world, the Date object into a string 
31 toUTCString ()    
 32  // the date portion of the string of Date transducer 
33 is  toDateString ()    
 34 is  // Returns string date format according to ISO standard 
35  toISOString ()    
 36  // The local time format, the date portion of the Date object conversion string of 
37 [  the toLocaleDateString ()    
 38 is  // the local time format, the time portion of the Date object into a string 
39  the toLocaleTimeString ()    
 40  // the local time format, the Date object into a string 
41 is  toLocaleString ()    
 42 is  / / the time portion of the Date object into a string 
43 is  toTimeString ()    
 44 is  // Date object of the year

Example 1

  Gets a specific value date

  Code:

 1 var times=new Date() 
 2         console.log(times.getDate());//
 3         console.log(times.getDay());//
 4         console.log(times.getFullYear());//
 5         console.log(times.getHours());//
 6         console.log(times.getMilliseconds());//毫秒
 7         console.log(times.getMinutes());//
 8         console.log(times.getMonth()+1);//月  +1
 9         console.log(times.getSeconds());// Day 
10          console.log (times.getTime ()); // 1970 to the present day the total number of milliseconds 
11          console.log (times.getYear ());

  as the picture shows:

Example 2

  Dynamic page display time today

  Code:

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>我的第一个JavaScript程序</title>
 6 <script>
 7 var a;
 8 window.onload= function(){
 9     a=window.setInterval("showTime()",1000);
10     showTime();
11 }
12     function showTime(){
13         var d=new Date();
14         var year=d.getFullYear();
15         var month=d.getMonth();
16         if(month<10){
17             month="0"+month;
18         }
19         var day=d.getDate();
20         if(day<10){
21             day="0"+day;
22         }
23      var hour=d.getHours();
24         if(hour<10){
25             hour="0"+hour;
26         }
27         var minutes=d.getMinutes();
28         if(minutes<10){
29             minutes="0"+minutes;
30         }
31         var seconds=d.getSeconds();
32         if(seconds<10){
33             seconds="0"+seconds;
34         }
35           document.getElementById("demo"). innerHTML =year+"-"+month+"-"+day+" "+hour+":"+minutes+":"+seconds;
36     }
37 </script>
38 </head>
39 
40 <body>
41 </body>
42 </html>

  as the picture shows:

                          Refresh time by setInterval 1 Miao

Math built-in objects

  (1) random values

    Ranging between 0 to 10

    Math.radom () * 1 + 10; the lower limit by multiplying by a factor of 1, 10 to the upper limit by Jia Fajia

    Ranging between 10 and 100

    Math.radom()*10+100;

    And so on.

  (2) down, rounding up

    Math.ceil () rounding up.

. 1  var X = 1.234 ;
 2  // ceiling function represents a greater than equal to x, and its nearest integer is 2 
. 3  var A = Math.ceil (X);
 . 4 the console.log (A); // 2

    Math.floor () rounds down.

. 1  var X = 1.234 ;
 2  // less than equal to x, and its nearest integer. 1 
. 3  var B = Math.floor (X);
 . 4 the console.log (B); // . 1

  (3) rounding

    console.log (Math.round (4.4)); rounded

    The result is 4;

    console.log (Math.round (4.5)); the five

    The result is 5;

  (4) the square root

    console.log(Math.sqrt(4));

    The result is 2;

  (5) power

    console.log (Math.pow (2,3)); 2 raised to, an index 3, 3 indicates a power of 2.

    The result is 8;

  (6) the absolute value

    console.log(Math.abs(-1));

    The result is 1;

  (7) Determine the maximum and minimum of two books

1  // selecting the maximum value of the minimum of the two numbers 
2 the console.log (Math.max (2,5)); // . 5 
. 3 the console.log (Math.min (2,5)); // 2

  (8) a random number Math.random ()

1 var ran = Math.random();
2 console.log(ran);[0,1)

 

Guess you like

Origin www.cnblogs.com/zhai113/p/11371932.html