Math
处理数学问题
特点:不需要创建对象,直接类型名方法
Math.方法()
1. 随机数random
Math.random()这个方法是用来生成一个0 ~ 1之间的随机数,每次执行生成的数字都不一样,但是一定是0 ~ 1之间的,生成的数字包含0 ,但是不包含1
var num = Math.random()
console.log(num) //得到一个随机数
2. 取整方法
- ceil
Math.ceil()是将一个小数向上取整得到的整数
var num = 10.1
console.log(Math. ceil(num)) // 11
var num2 = 10.9
console.log(Math.ceil(num2)) // 11
- floor
Math.f1oor()是将一个小数向下取整的到的整数
var nun = 18.1
console.1og(Math.floor(num)) // 10
var nun2 = 18.9
console.1og(Math.floor(num2)) // 10
- round
Math.round()是将一个小数四舍五入变成一个整数
var num = 10.1
console.log(Math.round(num)) // 10
var num2 = 10.6
console.log(Math.round(num2)) // 11
3. 绝对值
Math.abs()是返回一个数字的绝对值
var num = -10
console.log(math. abs(num)) // 10
4. 最大值、最小值
Math.max()/Math.min()得到的是你传入的几个数字之中最大/最小的那个数字
console.log(Math.max(1, 2.3,4, 5) //5
console.log(Math.min(1, 2.3,4, 5) //1
5. Math对象方法
- abs(x) 返回数的绝对值
- acos(x) 返回数的反余弦值
- asin(x) 返回数的反正弦值
- atan(x) 以介于-n/2与n/2弧度之间的数值来返回x的反正切值
- atan2(y,x) 返回从x轴到点(x,y) 的角度(介于-n/2与n/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对象的原始值
数值转化进制
十进制
数: 0 ~ 9 0123456789
满十进一 10 -> 十
八进制
数: θ ~ 7
满八进一 10 -> 八
二进制
数: 0 ~ 1
满二进一1 0 -> 二
十六进制
数: θ ~ 9 A B C D E F
满十六进一 10 -> 十六
0x表示十六进制,但是js会强制转换为十进制来运算,0xa==- 10
0开头表示八进制,同样强制转换为十进制来运算010== 8
- tostring()方法可以在数字转成字符串的时候给出一个进制数
语法: tostring(你要转换的进制)
var num = 100
console.log(num.tostring(2)) // 1100100
console.log(num.tostring(8)) // 144
console.log(num.tostring(16)) // 64
2. parseInt() 方法可以在字符串转成数字的时候,把字符串当成多少进制转成十进制
语法: parseInt(要转换的字符串,当作几进制来转换)
var str = 100
console.log(parseInt(str, 8)) // 64把100当作-一个八进制的数字转换成十进制以后得到的
console.log(parseInt(str, 16)) // 256把100当作十六进制的数字转换成十进制以后得到的
console.log(parseInt(str, 2)) // 4把100当作二进制的数字转换成十进制以后得到的
Date
js提供的内置构造函数,专门用来获取时间的
创建日期时间对象new Date()
new Date()在不传递参数的情况下是默认返回当前时间
var time = new Date()
console.log(time) //当前时间Fri Mar 01 2019 13:11:23 GMT+0800 (中国标准时间)
new Date()在传入参数的时候,可以获取到一个你传递进去的时间
var time = new Date('2019-03-03 13:11:11')
console.log(time) // Sun Mar 03 2019 13:11:11 GMT+0800 (中国标准时间)
Date常用方法
- getFullYear
getFullYear()方式是得到指定字符串中的哪一年
var time = new Date(2019 03,03,08,00,22)
console.log(time.getFullYear()) // 2019
- getMonth
getMonth()方法是得到指定字符串中的哪一个月份
var time = new Date(2019,03,03, 08,00,22)
console.log(time.getMonth()) // 3
注意:0-11表示月份,从0开始计数,0表示1月,11表示12月
- getDate
getDate()方法是得到指定字符串中的哪一天
var time = new Date(2019,03,03,08,00,22)
console.log(time.getDate()) // 3
- getHours
getHours()方法是得到指定字符串中的哪小时
var time = new Date(2019,03,03,08,00,22)
console.log(time.getHours()) // 8
- getMinutes
getMinutes()方法是得到指定字符串中的哪分钟
var time = new Date(2019,03,03,08,00,22)
console.log(time.getMinutes()) // 0
- getSeconds
getSeconds()方法是得到指定字符串中的哪秒钟
var time = new Date(2019,03,03,08,00,22)
console.log(time.getSeconds()) // 22
- getDay
getDay()方法是得到指定字符串当前日期是一周中的第几天(周日是0,周六是6)
var time = new Date(2019, 03,08,08,00, 22)
console.log(time.getDay()) // 1
- getTime
getTime()方法是得到执行时间到格林威治时间的毫秒数
var time = new Date(2019,03,08,08,00,22)
console.log(time.getTime()) // 1554681622000
日期时间格式化
/*
Thu Jul 29 2021 09:28:17 GMT+0800 (中国标准时间)
实际显示时间格式
2021-07-29 09:27:10
2021/07/2909:27:10
2021年7月29日9时27分10秒
*/
function getCurrentDateTime() {
var date = new Date() // 当前时间Thu Jul 29 2021 09:28:17 GMT+0800 (中国标准时间)
var year = date.getFullYear() //年份
var month = date.getMonth() + 1 //月份
var d1 = date.getDate() //日
var hours = date.getHours() //小时
var minutes = date.getMinutes() //分钟
var seconds = date.getSeconds() //秒
//格式化日期时间
var timeStr =`${year}-${month}-${d1} ${hours }:${ minutes}:${seconds}`
return timeStr
}
计算时间差
格林威治时间:1970年1月1日( 00:00:00 GMT )
-
先获取两个时间点到格林威治时间(1970年1月1日( 00:00:00 GMT ) )的毫秒数
-
两个时间相减,得到两个时间点之间相差的毫秒数
-
毫秒转换成天/时/分/秒等
var time1 = new Date('2019-01-01 00:00:00') var time2 = new Date('2019-01-03 10:01:00') var time = time2.getTime() - time1.getTime() // 毫秒差 // 换算一天 相差的总毫秒/ 1天的毫秒 = 相差的天数 var day = time/(1000 * 60 * 60 * 24) day = Math.floor(day) console.log(day) // 相差的总毫秒 - day天的毫秒 var hoursTime = time - day * (1000 * 60 * 60 * 24) var hours = hoursTime/(1000 * 60 * 60) hours = Math.floor(hours) console.log(hours) // 相差的小时的总毫秒 - hours的毫秒 var minutesTime = hoursTime - hours * (1000 * 60 * 60) var minutes = minutesTime/(1000 * 60) console.log(minutes)