内置函数的一些用法(math,date,时间戳)

130 阅读1分钟
 //math的方法
    // 求圆周率  console.log(Math.PI());
    //求最大值   
    //console.log(Math.max(20,30,));//30
    //求最小值  
    //console.log(Math.min(1,2,3,4));//1
    //绝对值
    // console.log(Math.abs(-1));//1

    //向下取整
    // console.log(Math.floor(2.1));//2

    //向上取整
    // console.log(Math.ceil(2.1));//3

    //四舍五入取整
    // console.log(Math.round(2.3));//2

    //随机数的一个方法
    //随机数的取值范围是大于0小于1;
    // console.log(Math.random());
    


    // function getrandom(min,mix){
    //     return Math.floor(Math.random()*(max-min))
    // }
    // //随机点名
    // var arr=["张三","李四","汪峰"];
    

    // function getrandom(max,min){
    //     return Math.floor(Math.random()*(max-min)+min);
    // }

    // var a=getrandom(1,10);
    // var b=0;

    // while(b<10){
      
    //     var c=Number(prompt("请输入数"));
    //     if(c>a){
    //         alert("您输入的数大了")
    //         b++;   
    //     }
    //     else if(c<a){
    //         alert("您输入的数小了");
    //         b++; 
    //     }
    //     else{
    //         alert("输入正确");
    //         break;
    //     }
    // }
   


    //date对象
    var date=new Date();

    //获取当前年份
    // console.log(date.getFullYear());

    //获取当前月份
     console.log(date.getMonth(2020-3-18)+1);

    //获取当前日;
    // console.log(date.getDate());

    //获取星期几
    // console.log(date.getDay());



   //获取当前时间
    // var date=new Date();
    // console.log(date);
    // 小时
    // console.log(date.getHours());
    // 分钟
    // console.log(date.getMinutes());
    // 秒
    // console.log(date.getSeconds());


    // function gettime(){
    //     var date=new Date();
    //     var hours=date.getHours();
    //     var minutes=date.getMinutes();
    //     var seconds=date.getSeconds();
    // }
    //1584529608702
    //1584529642785
     //时间戳
     //是从1970年1月1日0:0:0到某一时间的毫秒数;
     //var date=new Date();
     //1.获取时间戳
     //console.log(date.valueOf(2020-3-18));
     //2.获取时间戳
    //  console.log(date.getTime());
     //获取时间戳
    //  console.log(+new Date());
     //获取时间戳
    //  console.log(Date.now());