js基本语法09

内置对象:自带的对象 Math Array String Date

一、Math

Math是数学相关的运算, max min 底层原理就是for循环

Math是直接拿来就可以用的

如果数值里有非数字的值,取大小时会直接打印NaN

    //    console.log(Math.max(1,5,66,234,55));
    //    console.log(Math.min(1,5,66,234,55));
    //    console.log(Math.min(1,5,66,234,'ddd'));//NaN

/*        var max=Math.max(1,5,66,234,55)
       console.log(max);
       var min=Math.min(1,5,66,234,55)
       console.log(min); */

二、取整

floor 向下(地板)取整,无论小数多小都朝下取整,如果是负数时,取整后是值变小

ceil 向上(天花板)取整,无论小数多小都朝上取整,如果负数时,取整后是变大

round 四舍五入取整方式 负数一样四舍五入取整

abs 取绝对值

/*      console.log(Math.ceil(1.3));
        console.log(Math.ceil(1.3333));
        console.log(Math.ceil(1.9));
        console.log(Math.ceil(1.5));
        console.log(Math.ceil(1.01));

        console.log(Math.floor(1.3));
        console.log(Math.floor(1.3333));
        console.log(Math.floor(1.9));
        console.log(Math.floor(1.5));
        console.log(Math.floor(1.01));

        console.log(Math.ceil(-1.3));
        console.log(Math.ceil(-2.3333));
        console.log(Math.ceil(-1.9));
        console.log(Math.ceil(-1.5));
        console.log(Math.ceil(-3.01));

        console.log(Math.floor(-1.3));
        console.log(Math.floor(-3.3333));
        console.log(Math.floor(-1.9));
        console.log(Math.floor(-4.5));
        console.log(Math.floor(-1.01)); 
        */

        // console.log(Math.round(1.2));
        // console.log(Math.round(1.4));
        // console.log(Math.round(2.3));
        // console.log(Math.round(1.5));
        // console.log(Math.round(1.9));

        console.log(Math.round(-1.2));//-1
        console.log(Math.round(-1.4));//-1
        console.log(Math.round(-2.3));//-2
        console.log(Math.round(-1.5));//注:-1
        console.log(Math.round(-1.9));//-2

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

三、获取随机数

        //    random  是0-1之间的随机数
        console.log(Math.random())

        // 获取指定范围的随机整数

        function getRandom(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min
        }
        console.log(getRandom(11, 20))

四、Date日期对象 是一个构造函数,展示的是从1970年1月1日到目前为止的毫秒数

/*     console.log(new Date());//Tue May 30 2023 13:35:04 GMT+0800
       console.log(new Date('2023/5/30'));//Tue May 30 2023 00:00:00 GMT+0800

       console.log(+new Date());//1685425091818
       console.log(Date.now());//1685425091818 毫秒数 用的最多 */

       //格式化时间
       var date = new Date()
       console.log(date);
/*     console.log(date.getFullYear());
       console.log(date.getMonth()+1);//返回的月份会比实际月份小1 所以需要加1
       console.log(date.getDate()); */
       //2023-05-30
       
       var year = date.getFullYear()
       var month = date.getMonth()+1
    //    var month = date.getMonth()+1>10?date.getMonth()+1:'0'+(date.getMonth()+1)
       var date = date.getDate()
       
       
       
       month = month>10? month: '0'+month
       console.log('今天是'+year+'年'+month+'月'+date+'日');
       console.log(year+'-'+month+'-'+date);

    //    console.log(date.getDay()); //0代表星期天 1代表星期一 6代表星期六

五、封装时间函数

    <script>
        //封装时间函数
        function getTime(){
            var t = new Date()
            var y = t.getFullYear()//年
            var m = t.getMonth()+1>10?t.getMonth()+1:'0'+(t.getMonth()+1)//月
            var d = t.getDate()>10?t.getDate():'0'+(t.getDate())//日
            var h = t.getHours()>10?t.getHours():'0'+(t.getHours())//时
            var mt = t.getMinutes()>10?t.getMinutes():'0'+(t.getMinutes())//分
            var s = t.getSeconds()>10?t.getSeconds():'0'+(t.getSeconds())//秒
            return y+'-'+m+'-'+d+'   '+h+':'+mt+':'+s
        }
        console.log(getTime());//2023-05-30   23:45:25
    </script>

六、倒计时

    <script>
        var nowTime = +new Date() //现在的时间
        var oldTime = +new Date('2023-6-18 20:00:00')  //目标时间
        var time = (oldTime - nowTime) / 1000 //把毫秒数变成秒数
        var d = parseInt(time / 60 / 60 / 24)  //天数
        var h = parseInt(time / 60 / 60 % 24)  //小时
        var m = parseInt(time / 60 % 60)   //分钟
        var s = parseInt(time % 60) //秒
        console.log(d + '天' + h + '时' + m + '分' + s + '秒')//18天20时9分32秒
    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值