八、Math和Date
Math 和 Date
- Math 是 js 的一个内置对象,提供了一堆的方法帮助我们操作 数字
- Date 是 js 的一个内置对象,提供了一堆的方法帮助我们操作 时间
Math
random
、round
、abs
、ceil
、floor
、max
、min
、PI
Math.random()
,用来生成一个0 ~ 1
之间的随机数,生成的数字包含 0 ,但是不包含 11
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22var num = Math.random();
console.log(num); // 得到一个随机数
/* 应用场景:随机点名 */
var names = [
"娄豪杰",
"温亚帅",
"窦全磊",
"老许",
"刘旭",
"殷逸",
"张朗",
"罗慧",
];
var timer = setInterval(() => {
let index = Math.floor(Math.random() * names.length);
// console.log(names[index])
if (names[index] === "浩瀚") {
console.log(`${names[index]}:我要请大家吃饭!!!`);
clearInterval(timer);
}
}, 500);Math.round()
,四舍五入1
2
3
4let num1 = 4.4999;
let num2 = 4.5;
console.log(Math.round(num1)); // 4
console.log(Math.round(num2)); // 5Math.abs
,绝对值1
2var num = -10;
console.log(math.abs(num)); // 10Math.ceil
,是将一个小数 向上取整 得到的整数1
2var num = 10.1;
console.log(Math.ceil(num)); // 11Math.floor
,是将一个小数 向下取整 的到的整数1
2var num = 10.1;
console.log(Math.floor(num)); // 10Math.max
,得到的是你传入的几个数字之中 最大 的那个数字1
2var str = "1, 2, 3, 4, 5";
console.log(Math.max(str)); // 5Math.min
,得到的是你传入的几个数字之中 最小 的那个数字1
2var str = "1, 2, 3, 4, 5"
console.log(Math.min(str)) // 1Math.PI
,得到的是π
的值,也就是3.1415936...
1
2
3var num = Math.PI
console.log(num); // 3.141592653589793
// 因为计算机的计算精度问题,只能得到小数点后 15 位
Date
js
提供的内置构造函数,专门用来获取时间的
测试程序执行了多久
1
2
3
4
5
6var start = Date.now(); // 时间戳
for (let i = 0; i < 200000000; i++) {
console.log(i);
}
var end = Date.now();
console.log((end - start) / 1000 + "秒");日期对象转为时间戳
1
2
3var date = new Date();
console.log(date * 1); // 方式一
console.log(Number(date)); // 方式二时间戳转为日期对象
1
2var timestamp = 1616372410777;
console.log(new Date(timestamp)); // Mon Mar 22 2021 08:20:10 GMT+0800 (中国标准时间)获取当前时间并且格式化
1
2
3
4
5
6
7
8
9
10
11
12
13
14let date = new Date();
let year = date.getFullYear(),
month = date.getMonth() + 1,
day = date.getDate(),
hour = date.getHours(),
minute = date.getMinutes(),
second = date.getSeconds();
month < 10 ? (month = "0" + month) : month;
day < 10 ? (day = "0" + day) : day;
hour < 10 ? (hour = "0" + hour) : hour;
minute < 10 ? (minute = "0" + minute) : minute;
second < 10 ? (second = "0" + second) : second;
console.log(`${year}-${month}-${day} ${hour}:${minute}:${second}`);
// 2021-03-22 08:16:00封装格式化时间函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22function dateFormat(date, format = "YYYY-MM-DD HH:mm:ss") {
const config = {
YYYY: date.getFullYear(),
MM: date.getMonth() + 1,
DD: date.getDate(),
HH: date.getHours(),
mm: date.getMinutes(),
ss: date.getSeconds(),
};
config.MM < 10 ? (config.MM = "0" + config.MM) : config.MM;
config.DD < 10 ? (config.DD = "0" + config.DD) : config.DD;
config.HH < 10 ? (config.HH = "0" + config.HH) : config.HH;
config.mm < 10 ? (config.mm = "0" + config.mm) : config.mm;
config.ss < 10 ? (config.ss = "0" + config.ss) : config.ss;
for (const key in config) {
format = format.replace(key, config[key]);
}
return format;
}
// 使用方式
var date = new Date();
console.log(dateFormat(date, "YYYY-MM-DD HH:mm:ss"));Monment.js
日期处理类库- 依赖安装方式:npm install moment –save 或者 yarn add moment
1
2
3
4
5
6
7
8
9
10/* cdn引入使用方式 */
<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.9.0/moment.min.js">
// 获取当前时间
let curTime = moment().format('YYYY-MM-DD HH:mm:ss');
console.log(curTime) // 2021-03-22 12:00:00
// 传入时间进行格式化处理
let date = moment('1998-05-23 12:00:00')
console.log(date.format('YYYY-MM-DD HH:mm:ss')) // 1998-05-23 12:00:00
定时器的使用
setInterval
和setTimeout
语法:setInterval(func,time)
- 有两个参数,第一个参数是一个函数,第二个参数是时间间隔,单位是毫秒
- 每间隔 time 毫秒,会执行一次函数
- setInterval 的返回值是一个数字
1 | // 计时器每1秒执行一次 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HJ BLOG!