Math、Date内置对象方法整理

Math : 内置的对象(构造函数)静态属性或静态方法。
                  一、
                        Math.PI : 圆周率
                        console.log(Math.PI);
                  二、近似值
                        1. 四舍五入 :  Math.round()
                              注: 负数时, <= 0.5   舍去     > 0.5  进一
                              
                              console.log(Math.round(4.5));  //5
                              console.log(Math.round(4.4));   //4
                              console.log(Math.round(-4.5));  //-4
                              console.log(Math.round(-4.5000001)); //-5
                              console.log(Math.round(-4.4)); //-4
                              console.log(Math.round(-4.8)); //-5
                        2. Math.ceil() 向上取整
                              console.log(Math.ceil(4.1)); //5
                              console.log(Math.ceil(4.9)); //5
                              console.log(Math.ceil(-4.1)); //-4
                              console.log(Math.ceil(-4.9)); //-4
                        3. Math.floor() 向下取整
                              console.log(Math.floor(4.1)); //4
                              console.log(Math.floor(4.9)); //4
                              console.log(Math.floor(-4.1)); //-5
                              console.log(Math.floor(-4.9)); //-5
                        4. Math.abs()  取绝对值
                              console.log(Math.abs(3),Math.abs(-3));
                  三、求最值
                        1. Math.max() : 求最大值
                              console.log(Math.max(8,4,2,4,5,2));
                           Math.max.apply(null,数组)
                           var arr = [8,4,2,4,5,2];
                              console.log(Math.max.apply(null,arr));
                        2. Math.min() : 求最小值
                              console.log(Math.min(5,3,2,5,1,3));
                              var arr = [5,3,2,5,1,3];
                              console.log(Math.min.apply(null,arr));
                  四、随机数
                        Math.random() : 0~1之间的随机数,可能包含0,但一定不包含1
                        万能随机公式:
                        Math.floor(Math.random() * (max - min + 1) + min);
                        
                        console.log(randomInt(30,20));
                  
                        function randomInt(min,max){
                              if(min > max){
                                    var t = min;
                                    min = max;
                                    max = t;
                              }
                                                                                    
                              return Math.floor(Math.random() * (max - min + 1) + min);
                                                                              
                        }
                  五、求m的n次方  Math.pow(m,n)
                        onsole.log(Math.pow(2,16));
                  六、求一个数的开方  Math.sqrt()
                        console.log(Math.sqrt(12));
 
Date : 日期对象
                  一、如何创建日期对象?   浏览器显示的 为 外国月份 == 你设置的月份加1  /getFullYear获取到的为原来设置的
                        var date = new Date();
                  二、获取日期时间
                        1. 年: 日期对象.getFullYear()
                        2. 月: 日期对象.getMonth()
                        3. 日: 日期对象.getDate()
                        4. 星期:日期对象.getDay()
                        5. 时:  日期对象.getHours()
                        6. 分: 日期对象.getMinutes()
                        7. 秒 : 日期对象.getSeconds()
                        8. 毫秒: 日期对象.getMilliseconds()
                        9. 时间戳 : 从1970年1月1日0时整到现在的毫秒数。
                              日期对象.getTime()
                  三、设置日期时间
                        1. 年: 日期对象.setFullYear()
                        2. 月: 日期对象.setMonth()
                        3. 日: 日期对象.setDate()
                        4. 时: 日期对象.setHours()
                        5. 分: 日期对象.setMinutes()
                        6. 秒: 日期对象.setSeconds()
                        7. 毫秒: 日期对象.setMilliseconds()
                  四、如何显示本地格式的日期时间
                        日期对象.toLocaleString()    年月日 时分秒
                  五、如何显示本地格式的日期
                        日期对象.toLocaleDateString()      年月日
                  六、如何显示本地格式的时间
                        日期对象.toLocaleTimeString()      时分秒
posted @ 2019-02-27 14:01  代码走江湖  阅读(234)  评论(0编辑  收藏  举报