zoukankan      html  css  js  c++  java
  • 从零开始学习前端JAVASCRIPT — 4、JavaScript基础Math和Date对象的介绍

    Math对象的介绍


    1:Math对象

    Math 对象用于执行数学任务。并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。您无需创建它,通过把 Math 作为对象使用就可以调用其所有属性和方法。


     2:Math属性

    PI:返回圆周率(约等于3.14159)。


     3:Math方法

    Math.round(3.6);   // 四舍五入。

    Math.random();     // 返回大于等于0到小于1之间的随机数。

    // 随机数如何设定范围
    // 0 - 100(包含)之间的随机值—Math.round四舍五入
    console.log(Math.round(Math.random() * 100));
    // 0 - 99(包含)之间的随机值—Math.floor向下取整
    console.log(Math.floor(Math.random() * 100));
    // 1 - 100(包含)之间的随机值—Math.ceil向上取整
    console.log(Math.ceil(Math.random() * 100));
    // 100 - 1000(包含)之间的随机值
    // 求两个值之间的随机数
    /*function random(x, y) {
    	return Math.round(Math.random() * (y - x)) + x;
    }
    */
    console.log(Math.round(Math.random() * (1000 - 100)) + 100);
    

    Math.max(a, b);    // 返回较大的数值。

    Math.min(a, b);    // 返回较小的数值。

    Math.abs(num);     // 返回绝对值。

    Math.ceil(3.6);    // 向上取整。4

    Math.floor(3.6);   // 向下取整。3

    Math.pow(x, y);    // x的y次方。

    Math.sqrt(num);    // 开平方。


     4:三角函数复习

    Math.sin(x);       // x的正弦值,返回值在-1到1之间。

    Math.cos(x);       // x的余弦值,返回值在-1到1之间。


     

    附录: 

    勾股定理:

    var a = 3, b = 4;
    var c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    console.log(c);

    三角函数:

    var degree = 60;
    var rad = Math.PI / 180 * degree;
    console.log(Math.ceil(Math.sin(rad) * 10) / 10);
    console.log(Math.floor(Math.cos(rad) * 10) / 10); 

    Date日期对象的介绍

     


     

    1:日期对象

    定义:JS DATE使用UTC(国际协调时间)1970,1,1,0,0,0,0所经过的毫秒数。

    在JS中日期也是它的内置对象,所以我们要对日期进行获取和操作,必须实例化对象。


     2:创建日期对象

    var oDate=new Date();

    将会包含本地时间的信息,包括年月日时分秒 星期。

    可以传入参数的格式:

    1.“时:分:秒 月/日/年”、“月/日/年 时:分:秒”、“年/月/日”等字符串。

    var oDate = new Date('12:12:12 12/12/2017'); //“时:分:秒 月/日/年”
    var oDate = new Date('12/14/2017 12:12:12'); //“月/日/年 时:分:秒”
    var oDate = new Date('2017/12/14'); //“年/月/日”等字符串

    2.年,月,日,时,分,秒。注意不能加“”。月是从0开始算的。

    var oDate = new Date(2017, 11, 12, 13, 24, 36); // 0代表1 

    3.时间戳。

    var oDate = new Date(1516775284475); 

     3:日期对象获取信息的方法

    注:月份和星期都是从0开始计算的。

    getDate():从 Date 对象返回一个月中的某一天 (1 ~ 31)。

    getMonth():从 Date 对象返回月份 (0 ~ 11)。

    getFullYear():从 Date 对象以四位数字返回年份。

    getDay():从 Date 对象返回一周中的某一天 (0 ~ 6)。 其中0代表星期日。

    getYear():请使用 getFullYear() 方法代替。由 getYear() 返回的值不总是 4 位的数字!对于介于 1900 与 1999 之间的年份,getYear() 方法仅返回两位数字。对于 1900 之前或 1999 之后的年份,则返回 4 位数字!ECMAscript已经不再要求使用该函数

    getHours():返回 Date 对象的小时 (0 ~ 23)。

    getMinutes():返回 Date 对象的分钟 (0 ~ 59)。

    getSeconds():返回 Date 对象的秒数 (0 ~ 59)。

    getMilliseconds():返回 Date 对象的毫秒(0 ~ 999)。

    getTime():返回 1970 年 1 月 1 日至今的毫秒数。


     4:日期对象设置信息的方法

    setDate():设置 Date 对象中月的某一天 (1 ~ 31)。

    setMonth():设置 Date 对象中月份 (0 ~ 11)。

    setFullYear():设置 Date 对象中的年份(四位数字)。

    setYear() 请使用 setFullYear() 方法代替。如果 year 参数是两位的数字,比如 setYear(91),则该方法会理解为 1991。如果要规定 1990 年之前或 1999 年之后的年份,请使用四位数字。 ECMAscript已经不再要求使用该函数

    setHours():设置 Date 对象中的小时 (0 ~ 23)。

    setMinutes():设置 Date 对象中的分钟 (0 ~ 59)。

    setSeconds():设置 Date 对象中的秒钟 (0 ~ 59)。

    setMilliseconds():设置 Date 对象中的毫秒 (0 ~ 999)。

    setTime():以毫秒设置 Date 对象。


     5:关于日期对象的常用操作

    1:将日期格式化成字符串。

    2:将指定格式字符串转化成日期对象。

    3:日期字符串转为毫秒数。

    4:计算两个日期的时间差值。

    5:日期函数封装(dateUtil.js)(封装常见功能)


     6:延时器和定时器

    延时器:

    语法:setTimeout(函数或者代码串,指定的时间(毫秒));

    在指定的毫秒数后只执行一次函数或代码。

    清除延迟器:clearTimeout();

    var timer3 = setTimeout(function () {
    	console.log('蹦!');
    } , 3000);
    
    setTimeout(function () {
    	clearTimeout(timer3);
    }, 2000);
    

      setTimeout()调用函数的写法方式,也可去掉引号

    function print() {
    	console.log('你好');
    }
    setTimeout('print()', 3000);//此写法如果在window.onload调用,建议放到匿名函数,
    //涉及到作用域的问题,js运行到此报错,也可调整js引用的位置
    

      

      定时器:

    语法:setInterval(函数或者代码串,指定的时间(毫秒));

    按照指定的周期(毫秒)不断的执行函数或者是代码串。

    清除定时器:clearInterval(); 

    var num = 6;
    var timer = setInterval(function () {
    	console.log(--num);
    
    	if(num === 0) {
    		clearInterval(timer);
    	}
    }, 1000);
    

     

      

     

  • 相关阅读:
    ZOJ 2158 Truck History
    Knight Moves (zoj 1091 poj2243)BFS
    poj 1270 Following Orders
    poj 2935 Basic Wall Maze (BFS)
    Holedox Moving (zoj 1361 poj 1324)bfs
    ZOJ 1083 Frame Stacking
    zoj 2193 Window Pains
    hdu1412{A} + {B}
    hdu2031进制转换
    openjudge最长单词
  • 原文地址:https://www.cnblogs.com/witkeydu/p/8345085.html
Copyright © 2011-2022 走看看