js Math 与 Date

183 阅读3分钟

1.Math定义

Math第一个数学对象,不是一个数学函数
构造函数实例化对象是使用new的方式!!!  
Math的属性有 取最大值:max() 取最小值:min() 取圆周率PI 
取绝对值 abs() 向下取整数 floor() 向上取整数 ceil() 四舍五入round()
取随机数 random();0<=x<1;
        function getRandom(min,max){
        return Math.floor(Math.random()*(max-min+1)+min); 
    }

2.Math属性方法练习

    <script>
        // 取圆周率 PI
        console.log(Math.PI)//3.141592653589793
        // 取最大值:max()
        console.log(Math.max(1,39,4));//39
        //取最小值min
        console.log(Math.min(3,2,5))//2
        //取绝对值 abs()
        console.log(Math.abs(-1));//1
        console.log(Math.abs('-20'))//20 即有个隐式转换
        //向下取整floor()
        console.log(Math.floor(5.8));//5
        //向上取整ceil()
        console.log(Math.ceil(4.3))//5
        //四舍五入取值round()
        console.log(Math.round(5.6))//6
        console.log(Math.round(5.4))//5
        //随机数 random();0<=x<1;
        console.log(Math.random())//0.7412945265226716
    </script>

3.随机数练习

    <script>
        // 取随机数
        // random()取随机数 0<=x<1
        console.log(Math.random());//0.9066351935547357
        // 在33 - 100之间取随机数
        //1.先去随机数 
        Math.random()
        //2.因为随机数的范围为 0<=x<1 且取出多为小数
        Math.random()*(100-33)//范围0<=x<67
        Math.random()*(100-33) +33//范围 33<=x<100
        Math.random()*(100-33+1)+33//范围33<=x<101
        Math.floor( Math.random()*(100-33+1)+33);// 去除小数点后的数字
        console.log( Math.floor( Math.random()*(100-33+1)+33));//控制台输出
        //3.封装函数
        function getRandom(min,max){
            return Math.floor(Math.random()*(max-min+1)+min); 
        }
        console.log(getRandom(10,11))
        //4.随机点名
        var arr = ["曹鑫", "吉喆", "张嘉译"];
        
        console.log(arr[getRandom(0,arr.length-1)] );
    </script>

4.Date定义

☆☆☆☆☆☆☆☆☆☆☆
在Date(time)添加时间 Date()默认为添加当前时间
    <script>
    //Date是一个构造函数
    var date = new Date();
    console.log(date)
    var date = new Date(2020, 3, 18,10,12);
    console.log(date)
    var date = new Date("2020-03-18 10:12")
    console.log(date)
</script>

5.Date属性

//获取当前的年份 getFullYear()
☆☆☆☆☆☆☆☆☆☆☆
//获取当前的月份 getMonth()获取的值从0开始
//获取当前的日getDate()
☆☆☆☆☆☆☆☆☆☆☆
//获取当前周几getDay() 范围 0~6 0代表星期天
//获取当前的小时 getHours()
//获取当前的分钟 getMinutes()
//获取当前的秒 getSeconds()
    <script>
        var date = new Date();
        //获取当前的年份 getFullYear()
        console.log(date.getFullYear());
        //获取当前的月份 getMonth()获取的值从0开始
        console.log(date.getMonth()+1);
        //获取当前的日
        console.log(date.getDate());
        //获取当前周几 getDay() 范围 0~6 0代表星期天
        console.log(date.getDay());
        //获取当前的小时 getHours()
        console.log(date.getHours());
        //获取当前的分钟 getMinutes()
        console.log(date.getMinutes());
        //获取当前的秒 getSeconds()
        console.log(date.getSeconds());
    </script>

6.时间戳

6.1 时间戳是什么?

是从1970年1月1日0:0:0 到某一点时间点位置的总毫秒数!!!1秒=1000毫秒

6.2时间戳的定义

//获取时间戳的方式1 valueOf()
//获取时间戳的方法2 getTime()
//获取时间戳的方法3 使用+new Date()获取 这个+相当于调用valueof
//4. Date.now();

6.3利用时间戳来进行倒计时

 <script>
        // 2020-3-19 12:00:00
        //现在      3 18 10:36:42
        function countDown(time){
            var date1 = new Date();
            //当前时间戳
            var newTime = date1.valueOf();
            //将来时间戳
            var date2 = new Date(time);
            var willTime = date2.valueOf();

            var offSetTime = (willTime - newTime)/1000 //秒
            //剩余天数
            var day =parseInt(offSetTime/60/60/24) 
            //剩余小时
            var hours = parseInt(offSetTime/60/60 %24 )
            //剩余分钟
            var minutes = parseInt(offSetTime/60%60)
            //剩余秒
            var seconds = parseInt(offSetTime%60)
            day=day<10?'0'+day:day;
           hours=hours<10?'0'+hours:hours;
           minutes=minutes<10?'0'+minutes:minutes;
           seconds=seconds<10?'0'+seconds:seconds;
           return day + '-' +hours+'-' +minutes +'-'+seconds 
        }
        console.log(countDown('2020-3-19 12:00:00'))



    </script>