内置对象
查内置对象
https://developer.mozilla.org/zh-CN/
Math
得到一个两数之间的随机整数,包括两个数在内(MDN写好的)
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
Date
Date()是构造函数,需要new创建
参数写法
常用参数
案例
格式化时间
var data = new Date()
year = data.getFullYear()
month = data.getMonth() + 1
day = data.getDate()
week = data.getDay()
weekArr = [‘星期日‘, ‘星期一‘, ‘星期二‘, ‘星期三‘, ‘星期四‘, ‘星期五‘, ‘星期六‘]
h = data.getHours()
min = data.getMinutes()
s = data.getSeconds()
h = h < 10 ? ‘0‘ + h : h
min = min < 10 ? ‘0‘ + min : min
s = s < 10 ? ‘0‘ + s : s
time = year + ‘年‘ + month + ‘月‘ + day + ‘日 ‘ + weekArr[week] + ‘ ‘ + h + ‘:‘ + min + ‘:‘ + s
console.log(time);
倒计时
function countDown(time) {
var nowTime = +new Date()
var inputTime = +new Date(time)
var times = (inputTime - nowTime) / 1000
var d = parseInt(times / 60 / 60 / 24)
var h = parseInt(times / 60 / 60 % 24)
var min = parseInt(times / 60 % 60)
var s = parseInt(times % 60)
d = d < 10 ? ‘0‘ + d : d
h = h < 10 ? ‘0‘ + h : h
min = min < 10 ? ‘0‘ + min : min
s = s < 10 ? ‘0‘ + s : s
return d + ‘天‘ + h + ‘时‘ + min + ‘分‘ + s + ‘秒‘
}
console.log(countDown(‘2021-5-29 17:00:00‘));
Array
检测
增减数组元素
数组排序
查找索引号
数组去重
function unique(arr) {
var newArr = []
for (var i = 0, len = arr.length; i < len; i++) {
if (newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i])
}
}
return newArr
}
console.log(unique([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 7, 6, 5, 2]));