Math对象:
用于数学计算的,提供了一系列数学计算的api
Math对象和其他的对象使用上不一样
1. Math.random():
含义:获取一个随机数,[0,1) 包含0,不包含1,获取0的几率下
Math.random(0,1)
console.log(Math.random());
2. Math.floor(num):
向下取整
console.log(Math.floor(5.8));//5
3. Math.ceil(num):
向上取整
console.log(Math.ceil(5.8));//6
4. Math.round(num)
四舍五入
console.log(Math.round(5.8));//6
console.log(Math.round(5.4));//5
console.log(Math.round(5.5));//6
5. Math.abs(num)
取绝对值
console.log(Math.abs(5.5));//5.5
console.log(Math.abs(-5.5));//5.5
6. Math.pow(n,m)
n的m次方 (m个n相乘)
Math.pow(2,5)
console.log(Math.pow(2,3));//8
7. Math.sqrt(num)
对num开根号
console.log(Math.sqrt(81));//9
8. Math.max(num1,num2,num3,...)
取最大值
9. Math.min(num1,num2,num3)
取最小值
案例
功能需求:点击按钮,给div换色
换色:随机出来一个16进制的颜色 "#"+"abc344"
1. 定义一个字符串: '1234567890abcdef'
2. 先随机出来一个 字符
2.1 先随机一个下标 var num = parseInt(Math.random()*str.length)
2.2 下标对应的字符 str.charAt(num)
3. 循环6次,产生6个随机字符,拼到一起(+)
4. #拼接到前面
把上述步骤封装成一个函数,该函的功能实现一个随机的16进制的颜色
改变div的颜色的写法:
oDiv.style.background = 颜色
function getColor() {
var str = '1234567890abcdef';
var newstr = ""
for (var i = 1; i <= 6; i++) {
var num = parseInt(Math.random() * str.length)
newstr += str.charAt(num)
}
return "#" + newstr;
}
console.log(getColor());//打印该颜色
获取 按钮和div
点击按钮:
div改变颜色
var oDiv = document.getElementById('div')
var oBtn = document.getElementById('btn')
oBtn.onclick = function(){
oDiv.style.background = getColor(); // 1. 调用函数,2.作为颜色值赋值给background
// 点击一次按钮,就会调用一次函数
}
Date:处理日期和时间的对象,提供了一系列处理日期和时间的api
创建日期对象:
var date = new Date();//获取当S前的时间对象
var date = new Date("2021-12-12 00:00:00")
var date = new Date(2012,11,12,00,00,00) 获取指定时间的日期对象
获取年:
date.getFullYear(): 获取四位数年份的
console.log(date.getFullYear()); //获取年
获取月:
date.getMonth(): 获取月, 0 - 11
console.log(date.getMonth() + 1);//获取月
获取日:
date.getDate(): 获取
console.log(date.getDate());//获取日
获取小时:
date.getHours(): 获取小时
console.log(date.getHours());//获取时
获取分钟:
date.getMinutes(): 获取分钟
console.log(date.getMinutes());//获取分
获取秒:
date.getSeconds(): 获取秒
console.log(date.getSeconds());//获取秒
获取星期:
date.getDay(): 获取星期0-6 ,注意:0代表星期日
date.getTime(): 获取距离1970年1月1日00:00:00的毫秒值
date.toString() : 只是将日期对象转为字符
date.toLocalString(): 转为本地时间,字符串形式
需求:想获取2021-12-12 00:00:00距离当前本地时间的毫秒差
思路:1. 先获取双12距离1970年那个时间的毫秒值
1.1 创建双12的日期对象 var date1 = new Date('2021-12-12 00:00:00');
1.2 调用getTime()方法 获取双12距离1970年的毫秒
2. 再获取当前本地时间距离1970年那个时间的毫秒值
2.1 获取本地时间对象var date2 = new Date()
2.2 调用getTime()方法 获取本地时间距离1970年的毫秒
3. 获取双12距离当前时间的差
var cha = date1.getTime() - date.getTime();
4. 获取两个时间短的毫秒差,就可以进行时间换算
var date1 = new Date('2021-12-12 00:00:00');
var date = new Date();
console.log(date1.getTime());//获取双12距离1970年的毫秒
console.log(date.getTime());//获取当前时间距离1970年的毫秒值
// 获取双12距离当前时间的差
var cha = date1.getTime() - date.getTime(); //毫秒
console.log(cha); // 毫秒 1s = 1000ms
console.log(cha / 1000);//秒
console.log(cha / 1000 / 60);//分钟
console.log(cha / 1000 / 60 / 60);//小时
console.log(cha / 1000 / 60 / 60 / 24);//天
-------------------设置时间-------------------------
设置年:
date.setFullYear():设置四位数年份的
设置月:
date.setMonth():设置月, 0 - 11
设置日:
date.setDate():设置日
设置小时:
date.setHours():设置小时
设置分钟:
date.setMinutes():设置分钟
设置秒:
date.setSeconds():设置秒
设置星期:
date.setDay():设置星期0-6 ,注意:0代表星期日
var date3= new Date();//2021/12/09 上午11:09:20
// date3.setDate(12);//将当前的日期修改成12
date3.setDate(date3.getDate()+5)
// console.log(date3.getDate());
console.log(date3.toLocaleString()); //2021/12/14 上午11:09:20
元旦倒计时案例
获取倒计时的功能封装成一个函数:
参数:两个日期
返回值:返回一个字符串的形式 '还剩' + d + '天' + h + '小时' + m + '分钟' + s + '秒'
*/
function getCha(date1,date2) {
var cha = date1.getTime() - date2.getTime();//毫秒
//获取秒
var s = parseInt(cha / 1000 % 60); //余下了多少秒(0-59)
// 获取分
var m = parseInt(cha / 1000 / 60 % 60);//余下了多少分(0-59)
// 获取小时
var h = parseInt(cha / 1000 / 60 / 60 % 24);// 余下了多少时(0-23)
// 获取多少天
var d = parseInt(cha / 1000 / 60 / 60 / 24);
return '还剩' + d + '天' + h + '小时' + m + '分钟' + s + '秒';
}
var date1 = new Date('2022-01-01 00:00:00');
var date2 = new Date();
document.write(getCha(date1,date2));
定时器分为
间歇性定时器:每隔多久执行一次
超时定时器:多久后执行一次(执行一次)
间歇性定时器:
语法一:
// setInterval(function(){},毫秒为单位的时间)
setInterval(function(){},1000) :1s执行一次函数体
语法二:
setInterval(fn,1000)
function fn(){}
语法三:
setInterval("fn()",1000)
function fn(){}
function getCha(date1, date2) {
var cha = date1.getTime() - date2.getTime();//毫秒
//获取秒
var s = parseInt(cha / 1000 % 60); //余下了多少秒(0-59)
// 获取分
var m = parseInt(cha / 1000 / 60 % 60);//余下了多少分(0-59)
// 获取小时
var h = parseInt(cha / 1000 / 60 / 60 % 24);// 余下了多少时(0-23)
// 进行一个补0操作
s = s < 10 ? '0' + s : s;
h = h < 10 ? '0' + h : h;
m = m < 10 ? '0' + m : m;
// 获取多少天
var d = parseInt(cha / 1000 / 60 / 60 / 24);
return '还剩' + d + '天' + h + '小时' + m + '分钟' + s + '秒';
}
var otxt = document.getElementById('txt')
setInterval(function () {
var date1 = new Date('2022-01-01 00:00:00');
var date2 = new Date();
// document.write(getCha(date1, date2)); //不能这么写
otxt.value = getCha(date1, date2)
}, 1000)