JavaScript初级篇——Math与Date

235 阅读5分钟

一、Math

Math是一个对象,它具有数学常数和函数的属性和方法。跟数学相关的运算(求绝对值,取整、最大值等)可以使用 Math 中的成员。

  • Math对象上提供的计算要比直接在JavaScript实现的快得多,因为Math对象上的计算使用了JavaScript引擎中更高效的实现和处理器指令。(但使用Math计算的问题时精度会因浏览器、操作系统、指令集和硬件而异。)

1-1、Math对象的常用属性

Math.PI 圆周率

1-2、Math对象的常用方法

1-2-1、min() & max()

  • min()和max()方法用于确定一组数值中的最小值和最大值。这两个方法都接收任意多个参数:
let max = Math.max(1,2,3,4,5)
console.log(max) // 5

let min = Math.min(1,2,3,4,5)
console.log(min) // 1
  • 要知道数组中的最大值和最小值,可以使用扩展操作符:
let values = [1,2,3,4,5]
let max = Math.max(...values)
console.log(max) // 5
  • 直接使用Math.max()和Math.min()时分别返回-Infinity和Infinity
console.log(Math.max()); // -Infinity
console.log(Math.min()); // Infinity
  • 当给max()、min()传入非数值时,会根据相应的规则进行转化,转化成数字后才去取其中的最大值、最小值,若转换为NaN时,那么结果为NaN
console.log(Math.max('11', 3, '66')); // 66

console.log(Math.max('aaa', 1, 3)); // NaN

console.log(Math.max(true, null, 3)); // 3

1-2-2、舍入相关方法

常用的舍入相关方法包括:Math.ceil() 、Math.floor()、Math.round()

  • Math.ceil()方法始终向上舍入为最接近的整数
  • Math.floor()方法始终向下舍入为最接近的整数
  • Math.round()方法执行四舍五入
let num = -1.1;
console.log(Math.ceil(num)); // -1

let num2 = 1.2;
console.log(Math.ceil(num2)); // 2

let num3 = 1.1;
console.log(Math.floor(num3)); // 1

let num4 = -1.2;
console.log(Math.floor(num4)); // -2

console.log(Math.floor('-1.1')); // -2

console.log(Math.round(1.5)); // 2

console.log(Math.round(-1.5)); // -1

console.log(Math.round(-1.6)); // -2

console.log(Math.round('-1.9')); // -2

如果传入非数值时,也会进行相应的转换

1-2-3、random()

Math.random()会返回一个0~1范围内的随机数,包含0但不包含1。

相关技巧:

  1. 得到0~n之间的值
  • 可通过Math.random() * n

    • 如果包含n 可使用round
    • 如果不包含n可使用floor
  1. 得到5~10之间的值

    第一步,先得到0~5之间的值(想法把开头的值变为0,即-5):

    • Math.random() * 5 第二步,得到5~10之间的值
    • Math.random() * 5 + 5
  2. 得到2~17之间的值

    第一步,先得到0~15之间的值:

    • Math.random() * 15 第二步,得到2~17之间的值:

    • Math.random() * 15 + 2

// 封装一个函数来实现取几到几之间的值
function getNum (min, max) {
  return Math.round(Math.random() * (max - min) + min);
}

1-2-4、其他方法

  • Math.abs():绝对值
  • Math.pow(x, power):返回x的power次幂
  • Math.sin(x):返回x的正弦
  • Math.cos(x):返回x的余弦
  • Math.tan(x):返回x的正切

二、Date

Date与Math不同,Date是一个构造函数,获取时间前需要实例化:

2-1、实例化Date

2-1-1、不传递参数

直接返回当前系统的当前时间

console.log(new Date()) // Mon Dec 27 2021 20:48:04 GMT+0800 (中国标准时间)

2-1-2、传递参数

常用写法如下:

  • 传递数字,以逗号分隔

    • 如:new Date(2021, 09, 27) 或 new Date(2021, 9, 27)
    • 注意:月份是从0开始的,以下表示10月
console.log(new Date(2021,9,27)); // Wed Oct 27 2021 00:00:00 GMT+0800 (中国标准时间)
console.log(new Date(2021,09,27));// Wed Oct 27 2021 00:00:00 GMT+0800 (中国标准时间)
  • 传递字符串,格式为'yyyy-MM-dd HH:mm:ss'

    • 如:new Date('2021-09-27 08:08:08');
    • 如:new Date('2021-9-27 8:8:8');
console.log(new Date('2021-09-27 08:08:08'));// Mon Sep 27 2021 08:08:08 GMT+0800 (中国标准时间)
console.log(new Date('2021-9-27 8:8:8')); // Mon Sep 27 2021 08:08:08 GMT+0800 (中国标准时间)
  • 传递字符串,格式为'yyyy/MM/dd HH:mm:ss'

    • new Date('2021/09/15 08:08:08');
    • new Date('2021/ 9/15 8:8:8');
console.log(new Date('2021/09/27 08:08:08'));// Mon Sep 27 2021 08:08:08 GMT+0800 (中国标准时间)
console.log(new Date('2021/9/27 8:8:8')); // Mon Sep 27 2021 08:08:08 GMT+0800 (中国标准时间)

2-2、常用方法

2-2-1、getFullYear()

返回4位数年,如2021

2-2-2、getMonth() 注意是0~11

获取月份,0表示1月,11表示12月

2-2-3、getDate()

获取月份中的日,1~31

2-2-4、getHours()

获取日期中的时,0~23

2-2-5、getMinutes()

获取日期中的分,0~59

2-2-6、getSeconds()

获取日期中的秒,0~59

2-2-7、getDay()

获取当前日期是周几,0表示周日,6表示周六

2-3、获取毫秒值的几种方式

  1. valueOf()

Date类型的valueOf()方法不是返回字符串,这个方法被重写后返回的是日期的毫秒表示。

因此,大于号和小于号可以直接使用它返回的值

let date1 = new Date(2022, 0, 1)
let date2 = new Date(2022, 1, 1)
console.log(date1 < date2); // true
  1. getTime()
console.log(new Date().getTime()) // 1640610560105
  1. +new Date()
console.log(+ new Date()) // 1640610560105
  1. Date.now()
console.log(Date.now()) // 1640610560105