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毫秒是每隔一秒计时器执行一次
 

最新文章

  1. 接触PHP快4个月
  2. AC中保存数据与查询数据
  3. Linux 输出重定向>和>>的区别
  4. struts2--表单重复提交
  5. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
  6. flex使用buttonbar为viewstack添加导航功能
  7. 设计模式_Proxy_代理模式
  8. vijosP1289 老板娘的促销方案
  9. Lucene的Query类介绍
  10. Java面试题集(136-150)
  11. [LeetCode] Longest Palindrome Substring 具体分析
  12. Spring Cloud Zuul 添加 ZuulFilter
  13. 【原创】源码角度分析Android的消息机制系列(四)——MessageQueue的工作原理
  14. [JSOI2008]星球大战starwar BZOJ1015
  15. Codeforces 833D Red-Black Cobweb [点分治]
  16. JavaScript(JS)之简单介绍
  17. MyBatis ResultMap Assocation 返回属性为null的问题
  18. Unity5天空盒小黑点问题
  19. SpringBoot定制错误页面
  20. js中用来操作字符串的相关的方法

热门文章

  1. POJ - 2762 Going from u to v or from v to u? (强连通缩点+判断单向连通)
  2. springboot的Scheduled定时器不工作
  3. Silverlight中获取控件中子控件
  4. JavaScript的消息机制
  5. 为JAXB和response设置编码,解决wechat4j中文乱码
  6. 微信小程序选择器
  7. mysql-8.0.11-winx64.zip安装教程
  8. caffe python lmdb读写
  9. 使用Idea 配置maven
  10. PAT1076. Forwards on Weibo (30)