zoukankan      html  css  js  c++  java
  • (九)jsMath

    1.Math对象
    作用:用于执行数学任务,把Math作为对象就可以调用其方法和属性.
    eg:    typeof Math);
    2.Math属性
        PI:圆周率(约等于 3.1415926);
    eg:    console.log(Math.PI);
    3.Math方法
        Math.round()四舍五入
        Math.floor()向下取整
        Math.ceil()向上取整
        Math.max()取最大值
        Math.min()取最小值
        Math.abs()取绝对值
        Math.pow(x,y)取x的y次方
        Math.sqrt()开平方
        Math.random()取 0-1之间的随机数不包括1
    a)随机数扩展一:
    0 - 100(包含)之间的随机值.
        console.log(Math.round(Math.random()*100));
    0 - 99(包含)之间的随机值.
        console.log(Math.floor(Math.random()*100));
    1 - 100(包含)之间的随机值.
        console.log(Math.ceil(Math.random()*100));
    100 - 1000(包含)之间的随机值.
        console.log(Math.round(Math.random()*(1000 - 100) + 100));
    解析:Math.random() 0-1
        Math.round(Math.random()) 0-1包含1
        Math.round(Math.random()*900) 0-900;
        Math.round(Math.random()*900 +100) 100-1000;
    求两个值之间的随机数封装成一个方法
        function numRandom(x,y){
        console.log(Math.round(Math.random()*(y-x)+x));
        }
    4.勾股定理
    eg:    function(a,b){
        var c = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
        console.log(c);
    }
    function(a,b);
    5进制的转换
        console.log((153).toString(16));result 99  转换为16进制
        console.log((153).toString(8)); result 231 转换为8进制
    6.创建日期对象
    var oDate = new Date();输出的顺序为:week month date hours:minutes:second GMT+0800(中国标准时间);
    注:可以自定义输出的日期的格式输出结果为对象中的字符串,
    eg:    可以传入的参数格式为:“时:分:秒 月/日/年”、“月/日/年 时:分:秒”、“年/月/日”等字符串。年,月,日,时,分,秒。月是从0开始算的
        var oDate = new Date(12:12:12 11/12/2018);
        var oDate = new Date(11/12/2018 12:12:12);
        var oDate = new Date(2018/11/12);
        var oDate = new Date(2018,11,12,12,12,12);
    7.时间戳
    概念:从1970年0月0日0时0分0秒开始计算到某一刻时间的总毫秒数.
    8.获取日期的方法
    eg:    var oDate = new Date();获取日期对象
        console.log(oDate.getFullYear());获取oDate对象的    年份
        console.log(oDate.getMonth());获取oDate对象的    月份
        console.log(oDate.getDate());获取oDate对象的    日期
        console.log(oDate.getHours());获取oDate对象的    小时
        console.log(oDate.getMinutes());获取oDate对象的 分钟
        console.log(oDate.getSeconds());获取oDate对象的    秒
        console.log(oDate.getMilliseconds());获取oDate对象的    毫秒
        console.log(oDate.getTime());获取oDate对象的    时间戳 共13位.
        console.log(oDate.getDate());获取oDate对象的    周天 返回的值为0-6;0为周日
    9.设置日期的方法
    eg:    var oDate = new Date();获取日期对象
        oDate.setFullYear();设置oDate对象的    年份
        oDate.setMonth();设置oDate对象的    月份
        oDate.setDate();设置oDate对象的    日期
        oDate.setHours();设置oDate对象的    小时
        oDate.setMinutes();设置oDate对象的 分钟
        oDate.setSeconds();设置oDate对象的    秒
        oDate.setMilliseconds();设置oDate对象的    毫秒
        oDate.setTime();设置oDate对象的    时间戳 共13位.
        oDate.setDate();设置oDate对象的    周天 返回的值为0-6;0为周日
    10.常用日期工具
    a)将日期格式化成字符串  字符串的格式为  YYYY-MM-DD HH:II:SS
        function formatDateToString(){
            // 先获取对象日期
            var oDate = new Date();
            // 从该对象中分别拿出所需要的    年,月日,时,分,秒 并放到一个变量中存储起来
            var year = oDate.getFullYear();
            var month = oDate.getMonth()+1;
            var date = oDate.getDate();
            var hours = oDate.getHours();
            var minutes = oDate.getMinutes();
            var seconds = oDate.getSeconds();
            return year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
        }
        console.log(formatDateToString());//调用方法。输出结果为计算机当前时间,格式为2018-01-24 18:01:43;
    b)将日期格式的字符串转换成对象    格式为 YYYY-MM-DD HH:II:SS
    function formatStringToDate(str){
            //字符串的分割,丢弃所分割的字符并产生的字符串
            var oDate =str.split(" ");//[2018-01-24,15:01:43]
            //将日期分割成年份和时间两部分
            var left = oDate[0];//[2018-01-24]
            var right = oDate[1];//[18:01:43]
            left = left.split("-");//[2018,01,24]
            right = right.split(":");//[18,01,43]
            return new Date(left[0],left[1]-1,left[2],right[0],right[1],right[2]);
        }
        console.log(formatStringToDate('2018-01-24 18:01:43'));//此时输入的月份是比输出的月份大一的
    c)将字符串格式的日期转换成毫秒
        function formatStringToMilli(str){
             //因为日期的方法中有直接转换为毫秒的方法 oDate.getTime();所以现将字符串格式的日期转换为对象
             var oDate = formatStringToDate(str); // 调用前面所封装的将字符串转换为对象的方法
             return oDate.getTime();
        }
        console.log(formatStringToMilli('2018-01-24 18:01:43'));
    d)计算两个日期的差值.
        function diffDate(str1,str2){
             var
                 oDate1 = formatStringToDate(str1),
                 oDate2 = formatStringToDate(str2);
             return oDate2.getTime() - oDate1.getTime();
         }
         console.log(diffDate('2018-01-24 18:01:43','2018-01-24 18:01:44'));
    //解析思路:先用已经封装的方法b)转换为对象.然后使用日期自身的time方法,转换为毫秒后,用第二个所给的日期减去第一个所给的日期得出毫秒差
    11.延时器
    a)语法:var timer = setTimeout(function(){},time);
    eg:    setTimeout(function(){
            console.log("你好,狗子,你还是变了");
        },2000); //这里的2000是毫秒, 1s =1000ms;
    b)清除延迟器
    语法:clearTimeout(function(){},time);
    eg:    var timer = setTimeout(function(){
        console.log('你好,猴子,你还是变了');
    },3000);
        clearTimeout(function(){
            clearTimeout(timer);    
        },2000);
    12.计时器
    语法:var timer = setInterval(function(){},time);
    eg:    var num = 10;
        var timer = setInterval(function(){
            if(num == 0){
                clearInterval(timer);//当计时到0是清除计时
            }
            console.log(num--);
        },1000);// 这里的1000毫秒是每隔一秒计时器执行一次
     
  • 相关阅读:
    Linux Kernel 'b43'无线驱动本地特权提升漏洞
    Linux Kernel “dispatch_discard_io()”RO Disk Manipulation安全绕过漏洞
    Linux kernel ‘fill_event_metadata’函数资源管理错误漏洞
    Linux kernel ‘b43_request_firmware’函数格式化字符串漏洞
    phpcms 2007 onunload.inc.php update SQL注入漏洞
    出差到天津的计划
    Auguries of Innocence
    做一个东西要多久
    Wise Installation 9 的一个容易忽视的选项
    天津行总结路在嘴底下
  • 原文地址:https://www.cnblogs.com/bgwhite/p/9375561.html
Copyright © 2011-2022 走看看