2020-03-10

Math对象常见API(应用程序接口)

    -Math是 JavaScript 的原生对象(内建对象),提供各种数学功能。该对象不是构造函数,不能生成实例,所有的属性和方法都必须在Math对象上调用。

    Math对象的属性,提供以下一些数学常数

        Math.E:常数e。

        Math.PI:常数 Pi。


    Math.abs方法返回参数值的绝对值。

        eg: Math.abs(1) // 1

            Math.abs(-1) // 1

    Math.max方法返回参数之中最大的那个值,Math.min返回最小的那个值。如果参数为空, Math.min返回Infinity(正无穷大), Math.max返回-Infinity。

        eg: Math.max(2, -1, 5) // 5

            Math.min(2, -1, 5) // -1

            Math.min() // Infinity 

            Math.max() // -Infinity

    Math.floor方法小于参数值的最大整数(地板值/向下取整)。

        eg: Math.floor(3.2) // 3

            Math.floor(-3.2) // -4

    Math.ceil方法返回大于参数值的最小整数(天花板值/向上取整)。

        eg: Math.ceil(3.2) // 4

            Math.ceil(-3.2) // -3

    Math.round方法用于四舍五入

        -注:正数与负数略有不同

        eg: Math.round(0.1) // 0

            Math.round(0.5) // 1

            Math.round(0.6) // 1

            Math.round(-1.1) // -1

            Math.round(-1.5) // -1

            Math.round(-1.6) // -2

    Math.pow方法返回以第一个参数为底数、第二个参数为幂的指数值。

        eg: // 等同于 2 ** 2

            Math.pow(2, 2) // 4

            // 等同于 2 ** 3

            Math.pow(2, 3) // 8

    Math.sqrt方法返回参数值的平方根。如果参数是一个负值,则返回NaN。

        eg: Math.sqrt(4) // 2

            Math.sqrt(-4) // NaN

        注:勾股定理复习:a^2+b^2=c^2

    Math对象还提供一系列三角函数方法

        Math.sin():返回参数的正弦(参数为弧度值)

        Math.cos():返回参数的余弦(参数为弧度值)

        Math.tan():返回参数的正切(参数为弧度值)

            other: 30*Math.PI/180  角度转为弧度

Date对象:

    -JS中使用Date对象来表示时间

    创建一个Date对象,如果直接使用构造函数创建一个Date对象,则会封装为当前代码执行的时间

        var d=new Date(); 

        console.log(d)


    创建一个指定的时间对象,需要在构造函数中传递一个表示时间的字符串或毫秒数作为参数

        var d2 = new Date("12/03/2016 11:10:30")

        console.log(d2)

            -日期的格式  月份/日/年 时:分:秒

    Date对象方法:

        oDate.getDate()      返回一个月中的某一天 (1 ~ 31)

        oDate.getDay()      返回一周中的某一天 (0 ~ 6)

        oDate.getMonth()    返回月份 (0 ~ 11)

        oDate.getFullYear()  以四位数字返回年份

        oDate.getHours()    返回当前小时(0-23)

        oDate.getMinutes() 返回当前分钟 (0 ~ 59)

        oDate.getSeconds()      返回当前秒(0 ~ 59)

        oDate.getMillisenconds()  返回当前毫秒(0 ~ 999)

        oDate.getTime()     

            -获得当前日期对象的时间戳

            -时间戳,指的是从格林威治时间的1970年1月1日,0时0分0秒,到当前日期所花费的毫秒数

                eg: var d2 = new Date("12/03/2016 11:10:30")

                    var time=d2.getTime()

                    console.log(time)  //1970.1.1 到 2016.12.3 11:10:30所花费的毫秒数

        注:获取当前时间戳

            var timer=Date.now();

            console.log(time);

        oDate.setDate()      设置月中的某一天 (1 ~ 31)

        oDate.setMonth()    设置月份 (0 ~ 11)

        oDate.setFullYear()  设置年份(四位数)

        oDate.setHours()    设置小时(0-23)

        oDate.setMinutes()          设置分钟 (0 ~ 59)

        oDate.setSeconds()          设置秒(0 ~ 59)

        oDate.setMillisenconds()    设置毫秒(0 ~ 999)

        oDate.setTime()            设置1970年1月1日至今的毫秒数

        静态方法:Date.parse()

            -Date.parse方法用来解析日期字符串,返回该时间距离时间零点(1970年1月1日 00:00:00)的毫秒数。

你可能感兴趣的:(2020-03-10)