Math对象
js提供的一些数学方法
PI属性不需要加括号
console.log(Math.PI)
最大值
console.log(Math.max(12, 5, 7, 99, 103, 1))
最小值
console.log(Math.min(12, 5, 7, 99, 103, 1))
幂
console.log(Math.pow(2, 10)) 2的10次幂
开平方
console.log(Math.sqrt(10))
向上取整
console.log(Math.ceil(12.1))
向下取整
console.log(Math.floor(-12.9))
四舍五入
console.log(Math.round(-12.6)) // 负数 5进 6舍
绝对值
console.log(Math.abs(-5))
随机数
- 0 - 1 0会出现,1永远不会出现,最大值是0.9999999999999
// console.log(Math.random())
// 0 - 10
// console.log(parseInt(Math.random() * 10))
// 10 - 20
// console.log(parseInt(Math.random() * 10 + 10))
// 5 - 15 最大值-最小值=差 随机数*差值 + 最小值
// console.log(parseInt(Math.random() * 10 + 5))
函数方法:
function random(n, m) {
return parseInt(Math.random() * (m - n) + n)
}
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 (中国标准时间)
● new Date() 传递的参数有多种情况
a. 传递两个数字,第一个表示年,第二个表示月份
var time = new Date(2019, 00) // 月份从 0 开始计数,0 表示 1月,11 表示 12月
console.log(time) // Tue Jan 01 2019 00:00:00 GMT+0800 (中国标准时间)
b. 传递三个数字,前两个不变,第三个表示该月份的第几天,从 1 到 31
var time = new Date(2019, 00, 05)
console.log(time) // Sat Jan 05 2019 00:00:00 GMT+0800 (中国标准时间)
c. 传递四个数字,前三个不变,第四个表示当天的几点,从 0 到 23
var time = new Date(2019, 00, 05, 22)
console.log(time) // Sat Jan 05 2019 22:00:00 GMT+0800 (中国标准时间)
d. 传递五个数字,前四个不变,第五个表示的是该小时的多少分钟,从 0 到 59
var time = new Date(2019, 00, 05, 22, 33)
console.log(time) // Sat Jan 05 2019 22:33:00 GMT+0800 (中国标准时间)
e. 传递六个数字,前五个不变,第六个表示该分钟的多少秒,从 0 到 59
var time = new Date(2019, 00, 05, 22, 33, 55)
console.log(time) // Sat Jan 05 2019 22:33:55 GMT+0800 (中国标准时间)
f. 传入字符串的形式
console.log(new Date(‘2019’))
// Tue Jan 01 2019 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date(‘2019-02’))
// Fri Feb 01 2019 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date(‘2019-02-03’))
// Sun Feb 03 2019 08:00:00 GMT+0800 (中国标准时间)
console.log(new Date(‘2019-02-03 13:’))
// Sun Feb 03 2019 13:00:00 GMT+0800 (中国标准时间)
console.log(new Date(‘2019-02-03 13:13:’))
// Sun Feb 03 2019 13:13:00 GMT+0800 (中国标准时间)
console.log(new Date(‘2019-02-03 13:13:13’))
// Sun Feb 03 2019 13:13:13 GMT+0800 (中国标准时间)
获取具体时间
年
var year = now.getFullYear()
// 月 0 - 11 需要加一
var month = now.getMonth() + 1
// 日
var day = now.getDate()
// 周几
var week = now.getDay()
// 时
var h = now.getHours()
// 分
var m = now.getMinutes()
// 秒
var s = now.getSeconds()
// 毫秒 1000毫秒 = 1秒
var ms = now.getMilliseconds()
每秒进行
//setInterval(tick, 1000)
setInterval(function () {
// 构造函数
var now = new Date() // 当前计算机的时间
// 年
var year = now.getFullYear()
// 月 0 - 11
var month = now.getMonth() + 1
// 日
var day = now.getDate()
// 周几
var week = now.getDay()
// 时
var h = now.getHours()
// 分
var m = now.getMinutes()
// 秒
var s = now.getSeconds()
// 毫秒 1000毫秒 = 1秒
var ms = now.getMilliseconds()
oTxt.innerHTML = year + '年' + month + '月' + day + '日' + ' 周' + week + ' ' + toDou(h) + ':' + toDou(m) + ':' + toDou(s)
}, 1000)
自定义时间
设置未来时间
// new Date(年, 月, 日, 时, 分, 秒, 毫秒)
// var target = new Date(2022, 5, 18, 0, 0, 0, 0)
// console.log(target)
// 今天的某个时间
var target = new Date()
target.setHours(18, 0, 0, 0)
console.log(target)
时间差
未来时间
var target = new Date(2022, 9, 1, 0, 0, 0, 0)
// 当前时间
var now = new Date()
// 时间差 毫秒数
// 转换成秒
var reduce = parseInt((target - now) / 1000)
倒计时
function tick() {
// 未来时间
var target = new Date(2022, 9, 1, 0, 0, 0, 0)
// 当前时间
var now = new Date()
// 时间差 毫秒数
// 转换成秒
var reduce = parseInt((target - now) / 1000)
// 1分钟 = 60秒
// 1小时 = 3600秒
// 1天 = 86400秒
// 天
var day = parseInt(reduce / 86400)
// 时
var h = parseInt(reduce % 86400 / 3600)
// 分
var m = parseInt(reduce % 3600 / 60)
// 秒
var s = reduce % 60
// console.log(day + '天' + h + '小时' + m + '分' + s + '秒')
oTxt.innerHTML = day + '天' + h + '小时' + m + '分' + s + '秒'
}
tick() // 一上来就执行一次
setInterval(tick, 1000) // 过一秒以后再执行一次
钱币格式转换
function mFn(n) {
// console.log(n) // 00000001
// 把数字变成字符串,再变成数组并且是个反序
var arr = (n + '').split('').reverse()
// 准备一个新数组
var newArr = []
// 循环老数组,到3的倍数多存一个逗号”,“
for (var i = 0; i < arr.length; i++) {
newArr.push(arr[i]) // 把每个数字都存到新数字里面
if ((i + 1) % 3 === 0) { // 判断3的倍数
newArr.push(',')
}
}
console.log(newArr)
// 把新数组反转过来,变成字符串
var str = newArr.reverse().join('')
// console.log(str)
// 如果第一位是逗号”,“需要把它替换掉
if (str[0] === ',') {
str = str.replace(',', '') // 替换完要重新赋值
}
return str
}
console.log(mFn(10000000))
双色球
// 6个不能重复的红球
var arr = []
function random(n, m) {
return parseInt(Math.random() * (m - n) + n)
}
// for有问题,固定次数
// for (var i = 0; i < 6; i++) {
// var n = random(1, 34)
// if (arr.indexOf(n) === -1) {
// arr.push(n)
// }
// }
// console.log(arr)
function toDou(n) {
return n < 10 ? '0' + n : '' + n
}
while (arr.length < 6) {
// 随机一个数
var n = random(1, 34)
// 判断数组里有没有这个数字,没有存一下
if (arr.indexOf(n) === -1) {
arr.push(n)
}
}
console.log(arr)
// 获取这个元素
// 通过标签名获取一组元素
var aTxt = document.getElementsByTagName('p')
var oDiv = document.getElementById('div1')
// 红球
for (var i = 0; i < arr.length; i++) {
aTxt[i].innerHTML = toDou(arr[i])
}
// 篮球
oDiv.innerHTML = toDou(random(1, 17))