Math:特点:所有的方法都是直接通过类名来调用的
//Math.floor(参数):向下取整
console.log(Math.floor(1.3))//1
//Math.ceil(参数):向上取整
console.log(Math.ceil(1.6))//2
//Math.round(参数):四舍五入取整
console.log(Math.round(2.3))//2
console.log(Math.round(2.6))//3
//Math.sqrt(参数):开方
console.log(Math.sqrt(2))//4
//Math.pow(m,n):返回m的n次方
console.log(Math.pow(2,3))//8
//Math.min(1,-2,3,4...):返回最小值
console.log(Math.min(1,-2,3,4));
//Math.max(1,-2,3,4...):返回最大值
console.log(Math.max(1,-2,3,4));
//Math.abs(参数):返回绝对值
console.log(Math.abs(-10))//10
生成任意区间的随机数
function rand(min,max){
return Math.round(Math.random()*(max-min)+min);
}
日期对象:
var date = new Date();//获取当前时间
console.log(date);
//获取年
console.log(date.getFullYear());
//月 0~11
console.log(date.getMonth());
//日
console.log(date.getDate());
//星期几 0~6
console.log(date.getDay());
//时
console.log(date.getHours());
//分
console.log(date.getMinutes());
//秒
console.log(date.getSeconds());
通过字符串设置指定日期
格式字符串 "yyyyy-mm-dd"[,hh:mm:ss]
var date = new Date("2021-8-25,19:20:33")
时间戳:某个日期距离1970/1月/1日零点相差的毫秒数
Date.parse(日期格式字符串):返回该字符串距离标准时间相差的毫秒数
设置日期的方法
把get改成set
在设置时间时,常量的单位为日期操作数的单位
date.setHours(date.getHours()+2);
date.setDate(date.getDate()+2);
console.log(date.toLocaleString());
循环计时器
setInterval(回调函数,时间间隔,);返回值为关闭定时时间器的钥匙
var count = 0;
var fun = function(){
console.log(++count);
}
setInterval(fun,2000);
//关闭定时器的标记
clearInterval(定时器的钥匙)
var time = setInterval(function(){
console.log(++count);
if(count == 5){
clearInterval(time);
}
},1000);