Math和Date

Math和Date

  • Math是js中的一个内置对象,为我们提供了更多操作数字的方法

  • Date是js中的一个内置构造函数,为我们提供时间的获取方式

Math

random

  • 语法:Math.random( ) 随机生成0~1的随机数

    console.log(Math.random()*10);//0-10之间的随机数

  • 常用

    n~m之间的随机整数
    function getNum(n,m){
    return Math.floor(Math.random()*(m-n+1)+n);
    }
    console.log(getNum(0,255));

round

  • 语法:Math.round( ) 使用四舍五入的方式取整

    console.log(Math.round(1.4)); // 1

abs

  • 语法:Math.abs( ) 绝度值(去除负号)

    var num = -10;
    console.log(Math.abs(num));// 10

ceil

  • 语法:Math.ceil( ) 向上取整

    var num = 1.2
    console.log(Math.ceil(num));// 2

floor

  • 语法:Math.floor( ) 向下取整

    var num = 1.5
    console.log(Math.floor(num));// 1

max

  • 语法:Math.max( ) 数值中的最大值

    var arr = [19,2,3,4,10,3,35,5]
    console.log(Math.max(arr));// 35

min

  • 语法:Math.min( ) 数值中的最小值

    var arr = [19,2,3,4,10,3,35,5]
    console.log(Math.min(arr));// 2

Pl

  • 语法:Math.Pl 得到一个π的值

  • 注:这个的后面是没有小括号的且只能得到最多15位小数;

进制的转换

  1. toString( ) 方法可以使数字类型转出字符串的时候给出一个进制数

    • 语法:toString(进制数)

      var num = 100
      console.log(num.toString(2)); //输出二进制的时候为1100100
      console.log(num.toString(8));//输出八进制的时候为144
      console.log(num.toString(16));//输出十六进制的时候为64

  2. parseInt( ) 方法可以在字符串转成数字的时候把字符串当成多少进制转成十进制

    • 语法: parseInt(值,原进制); 值可以是数字类型也可以是字符串;(字符串有局限)

      var str = "100";
      console.log(parseInt(str,2)) // 4

Date

new Date( )

  • 在不传递参数的情况下是默认返回当前时间

var time = new Date()
console.log(time)//输出当前时间
  • 在传入参数的时候,可以获取到一个你传递进去的时间(需要固定的格式)

    var time = new Date('2019-03-03 13:11:11')
    console.log(time) // Sun Mar 03 2019 13:11:11 GMT+0800 (中国标准时间)

  • new Date( ) 传递的参数有多种情况

    1. 传递两个数字,第一个表示年,第二个表示月份

      var time = new Date(2019, 00) // 月份从 0 开始计数,0 表示 1月,11 表示 12月
      console.log(time) // Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)

    2. 传递三个数字,前两个不变,第三个表示该月份的第几天,从 1 到 31

      var time = new Date(2019, 00, 05) 
      console.log(time) // Sat Jan 05 2019 00:00:00 GMT+0800 (中国标准时间)

    3. 传递四个数字,前三个不变,第四个表示当天的几点,从 0 到 23

      var time = new Date(2019, 00, 05, 22) 
      console.log(time) // Sat Jan 05 2019 22:00:00 GMT+0800 (中国标准时间)

    4. 传递五个数字,前四个不变,第五个表示的是该小时的多少分钟,从 0 到 59

      var time = new Date(2019, 00, 05, 22, 33) 
      console.log(time) // Sat Jan 05 2019 22:33:00 GMT+0800 (中国标准时间)

    5. 传递六个数字,前五个不变,第六个表示该分钟的多少秒,从 0 到 59

      var time = new Date(2019, 00, 05, 22, 33, 55) 
      console.log(time) // Sat Jan 05 2019 22:33:55 GMT+0800 (中国标准时间)

    6. 传入字符串的形式

      console.log(new Date('2019')) 
      // Tue Jan 01 2019 08:00:00 GMT+0800 (中国标准时间)
      console.log(new Date('2019-02')) 
      // Fri Feb 01 2019 08:00:00 GMT+0800 (中国标准时间)
      console.log(new Date('2019-02-03')) 
      // Sun Feb 03 2019 08:00:00 GMT+0800 (中国标准时间)
      console.log(new Date('2019-02-03 13:')) 
      // Sun Feb 03 2019 13:00:00 GMT+0800 (中国标准时间)
      console.log(new Date('2019-02-03 13:13:')) 
      // Sun Feb 03 2019 13:13:00 GMT+0800 (中国标准时间)
      console.log(new Date('2019-02-03 13:13:13')) 
      // Sun Feb 03 2019 13:13:13 GMT+0800 (中国标准时间)

将日期字符串格式化成指定内容

  • 比如我们得到的时间字符串是 `Sun Feb 03 2019 13:13:13 GMT+0800 (中国标准时间)

  • 我指向得到这个日期中是那一年,我们就要靠截取字符串的形式得到

  • 但是现在 js 为我们提供了一系列的方法来得到里面的指定内容

getFullYear

  • getFullYear()获取的是哪一年

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getFullYear()) // 2019

getMonth

  • getMonth()获取的是月份

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getMonth()) // 3

    注:月份是从 0 开始数的; 0 表示 1月,1 表示 2月,依此类推

getDate

  • getDate()获取的是哪一天

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getDate()) // 3

getDay

  • getDay()获取的是星期几

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getDate()) // 1

getHours

  • getHours()获取的是时间

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getDate()) // 8

getMinutes

  • getMinutes()获取的是分钟

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getDate()) // 0

getSeconds

  • getSeconds()获取的是秒

    var time = new Date(2019, 03, 03, 08, 00, 22)
    console.log(time.getDate()) // 22

getMilliSeconds

  • getMilliSeconds()获取的是毫秒

  • 注:计算机一般都是使用毫秒(自己再通过换算)

getTime

  • getTime()获取的是格林威治标准时间的毫秒数

    var time = new Date(2019, 03, 08, 08, 00, 22)
    console.log(time.getTime()) // 1554681622000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值