JS的一些内置方法

一、内置函数Math

  1.Math

    1)Math.abs()  求绝对值

    2)Math.PI  圆周率

  2.求近似值:

    1)Math.round()  四舍五入(负数:  >0.5 进一  <=0.5 舍去)

    2)Math.ceil()  向上取整

    3)Math.floor()  向下取整

  3.求最值

    1)Math.max()  求最大值

    扩展:Math.max.apply(null,arr)

    2)Math.min()  求最小值

    扩展:Math.min.apply(null,arr)

  4.求随机数

    1)Math.random()  求随机数(0<=n<1)

    扩展:Math.floor(Math.random)*(max-min+1)+min

      function randomInt(min,max){

        if(min>max){

          var t = min;

          min = max;

          max = t;

        }

        return Math.floor(Math.random()*(max - min + 1)+ min);

      }

  5.求m的n次方

    1)Math.pow(m,n);

  6.求开方

    1)Math.sqrt(num);

二、date日期对象

  1.如何创建日期对象

    var date = new Date();

  2.获取日期时间

    1)获取年份  date.getFullYear()

    2)获取月份  date.getMonth()

    3)获取日  date.getDate()

    4)获取星期  date.getDay()

    5)获取小时  date.getHours()

    6)获取分钟  date.getMinutes()

    7)获取秒  date.getSeconds()

    8)获取毫秒  date.getMilliseconds()

    9)获取时间戳  date.getTime()

  3.时间戳:从1970年1月1日0时整到现在的毫秒数

  4.以本地格式显示

    1)以本地格式的字符串显示日期时间  date.toLocaleString()

    2)以本地格式的字符串显示日期  date.toLocaleDateString()

    3)以本地格式的字符串显示时间  date.toLocaleTimeString()

  5.设置日期时间

    1)设置年份  date.setFullYear()

    2)设置月份  date.set.Month()

    3)设置日  date.setDate()

    4)设置小时  date.setHours

    5)设置分钟  date.setMinutes()

    6)设置秒  date.setSeconds(

    7)设置毫秒  date.setMilliseconds()

    8)设置时间戳  date.setTime()

  6.new Date()传参方式

    1)var date = new Date(y,m,d,hh,mm,ss)

    2)var date = new Date(y,m,d)

    3)var date = new Date("m,d,y,hh,mm,ss")(英文传参)

    4)var date = new Date("m,d,y")(英文传参)

Why do you work so hard? Because the things I want to buy are expensive and the places I want to go are far away. The person I like is very excellent.
原文地址:https://www.cnblogs.com/liufuyuan/p/10368890.html