js的Math对象和日期对象,走过路过要不错过一下?
Math对象使用
JavaScript中的Math对象是一个内置对象,它提供了许多数学函数和常量,可以帮助开发者轻松地进行数学计算。Math对象不需要被实例化即可使用,可以直接通过Math对象调用其属性和方法。
下面是一些常见的Math对象属性和方法:
- Math.PI:代表圆周率π的值
console.log( Math.PI);// 3.141592653589793
-
Math.abs():返回一个数的绝对值。
// 绝对值 console.log( Math.abs(-1)); //1 console.log(Math.abs('-11'));// 11
-
Math.ceil():将一个小数向上取整。
console.log(Math.ceil(1.9));//2
-
Math.floor():将一个小数向下取整。
console.log(Math.floor(1.9));//1
-
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.pow():返回一个数的指定次幂。
console.log(Math.pow(2, 4));// 获取2的4次幂 16
-
Math.random():返回一个介于0和1之间的随机数。
//生成大于或等于min且小于max的随机值 Math.random() * (max - min) + min; //表示生成0到任意数之间的随机整数 Math.floor(Math.random() * (max + 1)); //表示生成1到任意数之间的随机整数 Math.floor(Math.random() * max + 1);
-
Math.round():将一个小数四舍五入
console.log(Math.round(1.5));//2 console.log(Math.round(-1.5));//-1(获取比较大的值) console.log(Math.round(-1.6));//-2
除了上述属性和方法,Math对象还提供了许多其他的属性和方法,例如三角函数、指数函数、对数函数等等
案例:猜数字游戏
//生成一个1-10之间的整数
var a = Math.floor(Math.random() * 10) + 1;
//输入一个数字
var b = prompt("请输入一个1-10之间的数字:")
//将输入的数字和生成的数比较,直到猜对为止
while (a != b) {
if (a < b) {
b = prompt("你猜大了,再猜一次");
} else if(a > b){
b = prompt("你猜小了,再猜一次");
}
}
alert("恭喜你,猜对了")
Math对象在实际开发中非常常用,例如可以用Math.random()方法生成随机数,用Math.pow()方法计算数的次幂,用Math.ceil()和Math.floor()方法进行数的取整等等。熟练掌握Math对象的使用可以帮助开发者更高效地完成数学计算相关的任务。
Date对象使用
Date对象是JavaScript中一个重要的内置对象,它用于处理日期和时间。在JavaScript中,Date对象可以用来创建和操作日期对象,包括获取当前日期和时间、设置日期和时间、计算日期和时间之间的差异等等。
创建Date对象
在JavaScript中,可以通过以下方式来创建一个Date对象:
1.使用new关键字创建一个新的Date对象:
var today = new Date();
var today2 = new Date(2022,01,01,10,57,55)//Tue Feb 01 2022 10:57:55 GMT+0800 (中国标准时间)
2.使用日期字符串创建一个新的Date对象:
var dateFromString = new Date("2022-01-01");
3.使用时间戳创建一个新的Date对象:
时间戳: 从某个固定的起点(1970年1月1日00:00:00 UTC)开始经过的秒数或毫秒数
var dateFromTimestamp = new Date(1640995200000);
Date对象的方法
Date对象有很多方法,以下是一些常用的方法:
- getDate():获取当前日期中的日(1-31)
var today = new Date();
var day = today.getDate();
console.log(day); // 输出当前日期中的日
- getMonth():获取当前日期中的月份(0-11,真实月份-1)
var today = new Date();
var month = today.getMonth();
console.log(month + 1); // 输出当前日期中的月份
- getFullYear():获取当前日期中的年份
var today = new Date();
var year = today.getFullYear();
console.log(year); // 输出当前日期中的年份
- getTime():获取当前日期的时间戳
var today = new Date();
var timestamp = today.getTime();
console.log(timestamp); // 输出当前日期的时间戳
- setDate():设置当前日期中的日
var today = new Date();
today.setDate(10); // 设置当前日期中的日为10
console.log(today); // 输出设置后的日期
- setMonth():设置当前日期中的月份
var today = new Date();
today.setMonth(10); // 设置当前日期中的月份为11月
console.log(today); // 输出设置后的日期
- setFullYear():设置当前日期中的年份
var today = new Date();
today.setFullYear(2022); // 设置当前日期中的年份为2022
console.log(today); // 输出设置后的日期
案例:倒计时
function countDown(time) {
var nowTime = +new Date//new Date().getTime()的缩写,返回当前时间戳
var inputTime = +new Date(time);//设置结束的时间戳
var times = (inputTime - nowTime) / 1000;//计算剩余的毫秒数,转换为秒数
var d = parseInt(times / 60 / 60 / 24);//计算天数
d = d < 10 ? '0' + d : d;
var h = parseInt(times / 60 / 60 % 24);//计算小时
h = h < 10 ? '0' + h : h;
var m = parseInt(times / 60 % 60);//计算分钟
m = m < 10 ? '0' + m : m;
var s = parseInt(times % 60)//计算秒
s = s < 10 ? '0' + s : s;
return d + '天' + h + '时' + m + '分' + s + '秒';
}
console.log(countDown('2023-4-26 10:00:00'));//08天10时19分18秒
(看到这了不点个赞再走?)