zoukankan      html  css  js  c++  java
  • math对象和date对象

    math对象的函数方法,记住Math首字母要大写

    console.log(Math.abs(-5));  //取绝对值
    
    console.log(Math.ceil(1.1));  //向上取舍
    
    console.log(Math.floor(1.1)); //向下取舍
    
    console.log (Math.round(2.4));//四舍五入
    
    console.log(Math.exp(1.1));  //返回e的x次幂
    
    console.log(Math.log(10));  //e为底
    
    console.log(Math.max(10,20));
    
    console.log(Math.min(10,20));
    
    console.log(Math.pow(2,3));
    
    console.log(parseInt(Math.random()*10)) ;//返回0-10 的伪随机数  

    随机数的拓展,取上限和下限,获取之间的随机数。

    Math.random()是获取一个0-1的数,

        var random=function(up,down)
        {
            return parseInt(Math.random() *(up-down+1)+down);
        }
        alert(random(100,50));
    

     

    date对象

    获取当前的时间

     console.log (Date());

    创建一个时间对象

        var d= new Date();
        console.log(d.getDate()); //返回几日
        console.log(d.getDay());
        console.log(d.getMonth()+1);  //取值范围为0到11
        console.log(d.getFullYear()); 
        console.log (d.getHours());
    alert(d.getTime()); //1970年1月1日至今的毫秒数 

    拓展,

         console.log('将time转换为string   '+d.toString());
         console.log('将time转换为日期string  '+d.toDateString());
         console.log('将time转换为时间string   '+d.toTimeString());

    小应用:倒计时

    <script>
      var zero=function (num)  //0-9的整数前面添加一个0
      {
        return num <10?('0'+num):num;
      }
    
      window.setInterval(   
        function()
        {
        var date1 =new Date();  //获取当前时间
        var date2 = new Date("2017/07/11");  //你要计的那天
    
        var get_time =date2.getTime()-date1.getTime(); //getTime获取1970到对象时间的毫秒数目
    
        days=Math.floor(get_time/(24*3600*1000)); //获取需要的天数
    
        hours=Math.floor(get_time%(24*3600*1000)/(3600*1000));//天数之外余下小时数
    
        mins=Math.floor(get_time%(24*3600*1000)%(3600*1000)/(60*1000));//小时余下的分钟数
    
        secends=Math.floor(get_time%(24*3600*1000)%(3600*1000)%(60*1000)/1000);//分钟余下的秒数
    
        document.body.innerHTML='2017年她的生日还有'+zero(days)+'天'+zero(hours)+'小时'+zero(mins)+'分'+zero(secends)+'秒';
    },1000
        )
    </script>

     

  • 相关阅读:
    安装jupyter_contrib_nbextensions库
    1.20
    架构之美阅读笔记01
    使用 netcat 数据源测试 Flume
    使用 Avro 数据源测试 Flume
    Tensorflow01-认识张量Tensor
    Spark06-RDD分区、缓存与Checkpoint讲解
    Spark05-RDD算子细谈
    Spark04-RDD入门
    Spark03-Scala面向对象和函数式编程
  • 原文地址:https://www.cnblogs.com/biyongyao/p/5836934.html
Copyright © 2011-2022 走看看