Js的Math和Date的使用方法

# Math

- Math 给我们提供了操作数据的一些方法, 是 JS 内置的一个对象

# 数学方法

- random / round / ceil / floor / abs

- random

- 语法: `Math.random()`

- 作用: 得到一个随机数, 每次生成的数字都不一样, 但一定是0~1之间的,包含0 不包含1

- round

- 语法: `Math.round(数字)`

- 作用: 将一个小数 四舍五入 变成整数

- ceil

- 语法: `Math.ceil(数字)`

- 作用: 将一个小数 向上取整

- floor

- 语法: `Math.floor(数字)`

- 作用: 将一个小数, 向下取整

- abs

- 语法: `Math.abs(数字)`

- 作用: 返回一个数字的绝对值

- sqrt / pow / max / min / PI

- sqrt

- 语法: `Math.sqrt(数字)`

- 作用: 求平方根

- pow

- 语法: `Math.pow(基数, 幂)`

- 作用: 返回基数的几次幂

- max

- 语法: `Math.max(数字1, 数字2, 数字3, 数字4)`

- 作用: 返回最大值

- min

- 语法: `Math.min(数字1, 数字2, 数字3, 数字4)`

- 作用: 返回最小值

- PI

- 语法: `Math.PI()`

- 作用: 得到 π

- 封装函数: 范围内的随机整数

# Date

- Date 给我们提供了操作时间的一些方法, 是 JS 内置的一个对象

# 时间对象

#### 时间对象的创建

- 语法: `var timer = new Date()`

- 不传参

- 默认返回当前时间

```

var timer = new Date()

console.log(timer)

// Fri Oct 07 2022 08:52:29 GMT+0800 (中国标准时间)

```

- 传参

0. 可以获取到一个你传递进去的时间

```

var timer = new Date('2022-01-06 12:12:12')

console.log(timer)

// Thu Jan 06 2022 12:12:12 GMT+0800 (中国标准时间)

```

2. 传递两个数字, 第一个为年, 第二个为月

```

var timer = new Date(2022, 00) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月

console.log(timer)

// Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)

```

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

```

var timer = new Date(2022, 00, 31) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月

console.log(timer)

// Mon Jan 31 2022 00:00:00 GMT+0800 (中国标准时间)

```

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

```

var timer = new Date(2022, 00, 31, 23) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月

console.log(timer)

// Mon Jan 31 2022 23:00:00 GMT+0800 (中国标准时间)

```

5. 传递五个数字, 前四个不变, 第五个为该小时的多少分钟, 0-59

```

var timer = new Date(2022, 00, 31, 23, 59) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月

console.log(timer)

// Mon Jan 31 2022 23:59:00 GMT+0800 (中国标准时间)

```

6. 传递六个参数, 前五个不变, 第六个表示该分钟的多少秒, 0-59

```

var timer = new Date(2022, 00, 31, 23, 59, 59) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月

console.log(timer)

// Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

```

7. 传递字符串的形式

```

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-28')) // Thu Feb 28 2019 08:00:00 GMT+0800 (中国标准时间)

console.log(new Date('2019-02-28 13:')) // Thu Feb 28 2019 13:00:00 GMT+0800 (中国标准时间)

console.log(new Date('2019-02-28 13:13:')) // Thu Feb 28 2019 13:13:00 GMT+0800 (中国标准时间)

console.log(new Date('2019-02-28 13:13:13')) // Thu Feb 28 2019 13:13:13 GMT+0800 (中国标准时间)

```

#### 将日期对象格式化成指定内容

- 我们得到的时间字符串是: `Thu Feb 28 2019 13:13:13 GMT+0800 (中国标准时间)`

- 我们得到这个日期中是那年或者那天, 需要靠截取字符串的形式得到

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

###### getFullYear

得到指定字符串中的那一年

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getFullYear()) // 2022

```

###### getMonth

得到指定字符串中的那一月, 月的计数从 0 开始

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getMonth()) // 0

```

###### getDate

得到指定字符串中的那一日

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getDate()) // 31

```

###### getHours

得到指定字符串的那小时

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getHours()) // 23

```

###### getMinutes

得到指定字符串中的那分钟

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getMinutes()) // 59

```

###### getSeconds

得到指定字符串中的那秒钟

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getSeconds()) // 59

```

###### getDay

获取当前日期是一周中的 第几天(周日是0, 周六是6)

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getDay()) // 1

```

###### getTime

按照 格林威治时间计算 从1970 年 1 月 1 日 0 时 0 分 0 秒 到现在(或指定日期)的毫秒数

```

var timer = new Date(2022, 00, 31, 23, 59, 59)

console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)

console.log(timer.getTime()) // 1554681622000

```

#### 设置相关

###### setFullYear()

- 语法: `时间对象.setFullYear(年份信息)`

- 作用: 修改该时间对象内的 年份信息

```

var time = new Date()

timer.setFullYear(2008)

console.log(timer.getFullYear())

```

###### setMonth()

- 语法: `时间对象.setMonth(月份信息)`

- 作用: 修改该时间对象内的 月份信息

```

var time = new Date()

timer.setMonth(2)

console.log(timer.getMonth())

```

###### setDate()

- 语法: `时间对象.setDate(日期信息)`

- 作用: 修改该时间对象内的 日期信息

```

var time = new Date()

timer.setDate(4)

console.log(timer.getDate())

```

###### setHours()

- 语法: `时间对象.setHours(小时信息)`

- 作用: 修改该时间对象内的 小时信息

```

var time = new Date()

timer.setHours(12)

console.log(timer.getHours())

```

###### setMinutes()

- 语法: `时间对象.setMinutes(分钟信息)`

- 作用: 修改该时间对象内的 分钟信息

```

var time = new Date()

timer.setMinutes(45)

console.log(timer.getMinutes())

```

###### setSeconds()

- 语法: `时间对象.setSecond(秒钟信息)`

- 作用: 修改该时间对象内的 秒钟信息

```

var time = new Date()

timer.setSeconds(20)

console.log(timer.getSeconds())

```

###### setMilliseconds()

- 语法: `时间对象.setMilliseconds(毫秒信息)`

- 作用: 修改该时间对象内的 毫秒信息

```

var time = new Date()

timer.setMilliseconds(857)

console.log(timer.getMilliseconds())

```

###### setTime()

- 语法: `时间对象.setTime(毫秒信息)`

- 作用: 使用时间戳信息直接定位时间对象

```

var time = new Date()

timer.setTime(1440004004531)

console.log(timer)

```

# 定时器

- 两种定时器的介绍和作用 ( setTimeout / setInterval )

- setInterval 计时器, 每间隔固定的时间执行一次 语法: setInterval(函数, 数字) 函数: 每间隔多少时间执行的代码 数字: 间隔时间, 单位是毫秒

- setTimeout 倒计时器, 在指定时间到达后, 执行一次

- 语法: setTimeout(函数, 数字) 函数: 一段时间后执行的代码 数字: 间隔时间, 单位是毫秒

- 定时器的返回值及意义

- 返回值不区分定时器种类, 用于表示你这个定时器是页面中的第几个定时器

- 作用: 用来关闭定时器

- 关闭定时器

- 不区分定时器种类, 只要给出正确的定时器返回值就可以关闭

- 语法:

- clearTimeout(定时器返回值)

- clearInterval(定时器返回值)

# 简单的代码异步执行机制

- 什么是非异步执行代码

- 按照从上到下的顺序, 从左到右的顺序, 依次执行每一行代码

- 上一行代码没有执行完毕, 不会执行下一行代码

- 什么是异步执行代码

- 当代码遇到异步任务的时候, 会把该代码放在 异步队列 内等待

- 所有的同步代码执行完毕后, 再开始执行 异步队列内的代码

- 什么是异步任务

- 两种定时器都是异步任务

- 可以理解为, 限制性定时器外面的代码, 然后执行定时器里边的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值