1.内置对象是什么
内置对象:自带的对象,Math,Array、String、Date
2.Math的最大值、最小值
Math是数学相关的运算,max,min,底层原理是for循环
Math是直接拿来就可以用
如果数值里面有非数字的值,取大小时会直接打印NaN
代码案列
<script>
var a = Math.max(1, 8, 5, 9, 4)
var b = Math.min(1, 8, 5, 9, 4)
console.log(a);
console.log(b);
</script>
3.Math的取整
<script>
// ceil 向上取整 无论小数多小,都朝上取整,如果负数取整会变大取整
console.log(Math.ceil(1.223));
console.log(Math.ceil(2.223));
console.log(Math.ceil(1.223));
console.log(Math.ceil(-1.223));
console.log(Math.ceil(-1.223));
console.log(Math.ceil(-1.223));
// 取整 floor 向下取整 无论小数多大,都朝下取整,如果负数取整,会变小取整
console.log(Math.floor(1.25));
console.log(Math.floor(2.25));
console.log(Math.floor(1.75));
console.log(Math.floor(-1.25));
console.log(Math.floor(-2.25));
console.log(Math.floor(-1.75));
// round 四舍五入
console.log(Math.round(1.6));
console.log(Math.round(1.8));
console.log(Math.round(1.4));
console.log(Math.round(1.25));
console.log(Math.round(-1.6));
console.log(Math.round(-1.8));
console.log(Math.round(-1.4));
console.log(Math.round(-1.25));
//abs 取绝对值
console.log(Math.abs(-1.5));
</script>
4.随机数及其使用
<script>
// random 是0-1的随机数
console.log(Math.random());
// 获取指定范围的随机整数
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
// getRandom()
console.log(getRandom(1, 10));
</script>
案列使用:10次机会抽奖随机数1-50
<script>
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
// console.log(getRandom(1, 50));
var random = getRandom(1, 50)
for (var i = 1; i <= 10; i++) {
var num = prompt('请输入1-50之间的数字,有10次机会') - 0
if (random == num) {
alert('你中奖了')
break
} else {
alert('很遗憾,再接再厉')
}
}
</script>
5.Date日期对象
1.// Date日期对象 是一个构造函数 展示的是从1977年1月1日到至今的毫秒数
console.log(new Date());
// 第一种写法
// console.log(new Date('2022/5/30'));
// 2
console.log(+new Date());//毫秒数 1685425113515
console.log(Date.now());//毫秒数 1685425113515
2.格式化时间
// 格式化时间
var date = new Date()
var year = date.getFullYear();//年
var month = date.getMonth() + 1;//返回的月份会比实际月份小1,所以需要加1
var day = date.getDate();//日
var hour = date.getHours()
var minute = date.getMinutes()
var second = date.getSeconds()
// 2023-05-30
month = month > 10 ? month : '0' + month
console.log('今天是' + year + '年' + month + '月' + day + '日');
console.log(year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second );
console.log(date.getDay());//0代表星期天,1代表星期一,6代表星期六
3.封装时间函数(可以直接套用获取现在的时间)
<script>
function getTime() {
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1 > 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
var day = date.getDate() > 10 ? date.getDate() : '0' + date.getDate()
var hour = date.getHours() > 10 ? date.getHours() : '0' + date.getHours()
var minute = date.getMinutes() > 10 ? date.getMinutes() : '0' + date.getMinutes()
var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
}
var newdate = getTime()
alert(newdate)
</script>
4.倒计时及使用案列
<script>
function getTime() {
var date = new Date()
var year = date.getFullYear()
var month = date.getMonth() + 1 > 10 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1)
var day = date.getDate() > 10 ? date.getDate() : '0' + date.getDate()
var hour = date.getHours() > 10 ? date.getHours() : '0' + date.getHours()
var minute = date.getMinutes() > 10 ? date.getMinutes() : '0' + date.getMinutes()
var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
}
var newdate = getTime()
alert(newdate)
</script>
454

被折叠的 条评论
为什么被折叠?



