数学:
圆周率 Math.PI
console.log( Math.PI );
生成随机数 Math.random()
console.log( Math.random );
向上取整 Math.ceil(数字) - 获取比这个小数大的第一个整数
console.log( Math.ceil(3.3 );
向下取整 Math.floor(数字) - 获取比这个小数小的第一个整数
console.log( Math.floor(3.98) );
四舍五入 取整 Math.round(数字)
console.log( Math.round(5.6) );
求最大值 - Math.max(多个数字)
console.log( Math.max(1,5,9,7,3,4,6,2,8) );
求最小值 - Math.min(多个数字)
console.log( Math.min(1,5,9,7,3,4,6,2,8) );
求绝对值 - Math.abs(数字)
console.log( Math.abs(-3) );
求次方 - Math.pow(底数, 幂)
console.log( Math.pow(2, 3) ); // 2的3次方
开平方根 - Math.sqrt(数字)
console.log( Math.sqrt(4) );
求正弦值 - Math.sin(弧度) - 弧度 = 角度 * π / 180
console.log( Math.sin(30 * Math.PI / 180) );
求余弦值 - Math.cos(弧度)
console.log( Math.cos(60 * Math.PI / 180) );
求随机数 - Math.random()
console.log( Math.random() ); // 包0不包1的小数
需要包0不包10的随机整数
// console.log( Math.floor(Math.random() * 10) );
包5不包15的整数
// console.log( Math.floor(Math.random() * 10) + 5 );
包20不包100的整数
// console.log( Math.floor(Math.random() * 80) + 20 );
封装一个求某个范围内的随机整数的函数
function getRandom(a, b) {
var max = a
var min = b
if(a < b) {
max = b
min = a
}
var num = Math.floor(Math.random() * (max - min)) + min
return num
}
function getRandom(a, b) {
return Math.floor(Math.random() * Math.abs(a - b)) + Math.min(a, b)
}
var n = getRandom(3, 1)
console.log(n);
进制转换:
将其他进制转成10进制 --- parseInt(数据, 其他进制数)
将10机制转成其他进制 --- 数字.toString(其他进制数)
时间日期:
js内部提供了一个函数,用于创建时间日期对象 - Date
所有时间日期相关操作都需要通过这个时间日期对象进行
时间日期对象在输出的时候,默认会调用一个转字符串的方法输出 - 方便我们查看
创建对象:
当前时间:
new Date()
指定时间:
new Date(年,月,日,时,分,秒)
new Date(时间戳)
new Date('年-月-日 时:分:秒')
获取具体时间日期
获取年份 - getFullYear()
获取具体的月份 - getMonth() 使用0~11来描述1~12月
获取日期 - getDate()
获取周几 - getDay()
获取小时 - getHours()
获取分钟 - getMinutes()
获取秒 - getSeconds()
获取毫秒 - 1s = 1000ms
getMilliseconds()
获取时间戳 - 用毫秒数来描述当前时间的 - 1970年1月1日0点0分0秒到此时走过的毫秒数
getTime()
设置具体的事件日期
年 - setFullYear()
月 - setMonth()
日 - setDate()
时 - setHours()
分 - setMinutes()
秒 - setSeconds()
毫秒 - setMilliseconds()
时间戳 - setTime()
格式化
对象.toLocaleString()
对象.toLocaleTimeString()
对象.toLocaleDateString()
时间戳的获取
+new Date()
new Date().getTime()
Date.parse('具体的时间')