Math
处理数学问题,特点:不需要创建对象,直接类名.方法
Math.方法()
随机数random
-Math.random()这个方法是用来生成0~1之间的随机数
-每次执行生成的数字都不一样,但是一定是0~1之间的
-生成的数字包含0,但是不含1
var num =Math.random()
console.log(num)//得到一个随机数
取整方法round,ceil,floor
Math.ceil()时将是一个小数 向上取整 得到的整数
var num =10.1
console.log(Math.ceil(num))//11
var num2=10.9
console.log(Math.ceil(num2))//11
Math.floor() 是将一个小数 向下取整 的到的整数
var num=10.1
console.log(Math.floor(num))//10
var num2 =10.9
console.log(Math.floor(num2))//10
Math.round() 是将一个小数 四舍五入 变成一个整数
var num =10.1
console.log(Math.round(num))//10
var num2 =10.9
console.log(Math.round(num2))//11
Math.abs() 是返回一个数字的绝对值
var num = -10
console.log(Math.abs(num))//10
Math.max()得到的是你传入的几个数字之中最大的那个数字
console.log(Math.max(1,2,3,4,5))//5
Math.min()得到的是你传入的几个数字之中最小的那个数字
console.log(Math.min(1,2,3,4,5))//1
Math对象的方法
abs(x) 返回数的绝对值
acos(x)返回数的反余弦值
asin(x)返回数的反正弦值
atan(x)以介于-π/2与π/2弧度之间的数值来返回x的反正切值
atan2(y,x) 返回从x轴到点(x,y)的角度(介于-π/2与π/2弧度之间)
ceil(x)对数进行上舍入
cos(x) 返回数的余弦
exp(x) 返回e的指数
floor(x) 对数进行下舍入
log(x) 返回数的自然对数(底为e)
max(x,y) 返回x和y中的最大值
min(x,y) 返回x和y中的最小值
pow(x,y) 返回x的y次幂
random() 返回0~1之间的随机数
round(x) 把数四舍五入为最接近的整数
sin(x) 返回数的正弦
sqrt(x)返回数的平方根
tan(x) 返回角的正切
toSource() 返回该对象的源代码
valueOf() 返回Math对象的原始值
案例
生成10000个随机数0~10,统计每个随机数出现次数
function random1(){
var obj={}
for(var i=0;i<10000;i++){
var num=Math.ceil(Math.random()*10)
if(obj[num]){
obj[num]++
}else{
obj[num]=1
}
}
for( var key in obj){
console.log('key',key,'value',obj[key])
}
}
random1()
Date
js提供的内置构造函数,专门用来获取时间的
创建时间的对象为new Date()
var time = new Date() //在不传递参数的情况下默认为返回当前的时间
console.log(time) //当前时间Sat Aug 27 2022 10:12:50 GMT+0800 (中国标准时间)
//new Date() 在传入参数的时候,可以获取到一个传递进去的 时间
var timer = new Date('2022-08-26 09:09:12')
console.log(timer) //输出时间为Fri Aug 26 2022 09:09:12 GMT+0800 (中国标准时间)
new Date()传递的参数有多种情况
// 1.传递两个数字,第一个表示年,第二个表示月份
var time =new Date(2022,00)//月份从0开始计数,0表示1月 , 11表示12月
console.log(time)//Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
// 2.创建三个数字,前两个不变,第三个表示该月份的第几天,从1到31
var time1= new Date(2022,00,22)
console.log(time1)//Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
// 3.传递四个数字,前三个不变,第四个表示当天的几点,从0到23
var time2=new Date(2022,00,22,22)
console.log(time2) //Sat Jan 22 2022 22:00:00 GMT+0800 (中国标准时间)
// 4.传递五个数字,前四个不变,第五个表示的是该小时的多少分钟,从0到59
var time3=new Date(2022,00,22,22,23)
console.log(time3) //Sat Jan 22 2022 22:23:00 GMT+0800 (中国标准时间)
// 5.传递六个数字,前五个不变,第六个表示该分钟的多少秒,从0到59
var time4=new Date(2022,00,22,22,23,55)
console.log(time4)//Sat Jan 22 2022 22:23:55 GMT+0800 (中国标准时间)
// 6.传入字符串形式
console.log(new Date('2022'))
//Sat Jan 01 2022 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date('2022-09'))
//Thu Sep 01 2022 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date('2022-09-27'))
//Tue Sep 27 2022 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date('2022-09-27 10:'))
//Tue Sep 27 2022 10:00:00 GMT+0800 (中国标准时间)
console.log(new Date('2022-09-27 10:13'))
//Tue Sep 27 2022 10:13:00 GMT+0800 (中国标准时间)
console.log(new Date('2022-09-27 10:13:23'))
//Tue Sep 27 2022 10:13:23 GMT+0800 (中国标准时间)
常用方法
var time =new Date()
console.log(time.getFullYear())
//2022 获取到年份
var time1 =new Date()
console.log(time1.getMonth())
//7 获取到该字符串月份
var time2 =new Date()
console.log(time2.getDate())
//27 获取到该字符串具体哪一天
var time3 =new Date()
console.log(time3.getHours())
//10 获取到该字符串的具体哪个小时
var time4 =new Date()
console.log(time4.getMinutes())
//38 获取到该字符串的具体哪一分钟
var time5 =new Date()
console.log(time5.getSeconds())
//22 获取到该字符串的哪一秒钟
var time6 =new Date()
console.log(time6.getDay())
//6 获取到该字符串该周的第几天
var time8 =new Date()
console.log(time8.getTime())
//1661567902775 获取到该字符串的格林威治时间秒数
日期时间格式化
function getCurrentDateTime(){
var date=new Date() // 获取当前时间
var year=date.getFullYear() //获取当前年份
var month=date.getMonth() +1 //获取当前月份
var dl =date.getDate() //获取当前日数
var hours=date.getHours() //获取当前小时时间
var minutes=date.getMinutes() //获取当前分钟数
var seconds=date.getSeconds() // 获取当前秒数
// 格式化日期时间
var timeStr=`${year}-${month}-${dl} ${hours}:${minutes}:${seconds}`
// return timeStr
console.log(timeStr)//2022-8-27 11:4:37
}
getCurrentDateTime()
计算时间差的案例
function getTimeByBirth() {
var time1 = new Date('1960-01-01 00:00:00 ')
var currentTime = new Date()//获取到当前时间
var differentTime = currentTime.getTime() - time1.getTime() /*获取到当前时间与出生时间的时间差*/
//计算天数,先使用时间差除以一天所需的时间获取到天数在使用math得到整数
var day = differentTime / (1000 * 60 * 60 * 24)
day = Math.floor(day)
console.log(day)
//计算小时时间差减去天数,获得剩下的时间差,在获取到小时整数
var hourstime = differentTime - day * (1000 * 60 * 60 * 24)
var hours = hourstime / (1000 * 60 * 60)
hours = Math.floor(hours)
console.log(hours)
//计算分,小时时间差减去小时整数,获得分钟时间差,再用分钟时间差得到分钟整数
var minutestime = hourstime - hours * (1000 * 60 * 60)
var minutes = minutestime / (1000 * 60)
minutes = Math.floor(minutes)
console.log(minutes)
//计算秒数,分钟时间差减去分钟整数,得到秒数
var secondsTime = minutestime - minutes * (1000 * 60)
var seconds = secondsTime / 1000
seconds = Math.floor(seconds)
console.log(seconds)
document.write(`出生到现在经历了${day}天${hours}小时${minutes}分${seconds}秒`)
}
getTimeByBirth()