javascript中Date 日期时间对象 + Math 数学对象
日期时间对象
1、创建日期时间对象
var d = new Date();
2、日期时间对象转为字符串
d.toLocaleString() 整个日期时间转为字符串
d.toLocaleDateString() 日期转为字符串
d.toLocaleTimeString() 时间转为字符串
5. Math.max(x,y)
6. Math.min(x,y)
d.toLocaleDateString() 日期转为字符串
d.toLocaleTimeString() 时间转为字符串
3、获取年份
getYear()
获取两位数的年
console.log(d.getYear());
4、获取年份
getFullYear()
获取完整的四位数的年,使用的比较多
console.log(d.getFullYear());
5、获取月份
getMonth()
获取月份,返回值为0-11,表示1月到12月,所以获取到月份之后需要+1
console.log(d.getMonth()+1);
6、获取天
getDate()
获取天,返回值为今天是几号
console.log(d.getDate());
7、获取时
getHours()
console.log(d.getHours());
8、获取分
getMinutes()
console.log(d.getMinutes());
9、获取秒
getSeconds()
console.log(d.getSeconds());
10、获取时间戳
getTime()
console.log(d.getTime());
1970 年 1 月 1 日至今的毫秒数
时间戳专门用来计算时间的差值,或者倒计时等功能
<script>
function timer(){
var now = Date.now();
var future = new Date("2024-10-10 12:12:12");
// 时间差[毫秒]
var cha = future - now; // 两个时间相减,获取的是时间戳(毫秒)
if(cha>0){
var allS = parseInt(cha / 1000); // 1秒 == 1000毫秒
// 秒
var s = allS % 60;
// 分钟
var m = parseInt(allS/60)%60;
// 小时
var h = parseInt(allS/60/60)%24;
// 天
var d = parseInt(allS/60/60/24);
box.innerHTML = `距离指定时间还有${d}天${h}小时${m}分钟${s}秒钟`
}else{
console.log('计时结束');
}
}
setInterval(timer,1000)
</script>
// 计算差值
function getTime(){
var nowTime = new Date();
var endTime = new Date("2026-8-22 8:30:00");
var cha = endTime - nowTime;
if(cha <= 0){
alert("输入时间有误!");
// 终止定时器的方法
clearInterval(timer);
// 终止函数的执行
return;
}
// 毫秒转为秒 1000
var allMiao = parseInt(cha / 1000);
// 秒
var miao = allMiao % 60;
// 所有的分钟
var allFen = parseInt(allMiao / 60);
// 分钟
var fen = allFen % 60;
// 所有小时
var allHour =parseInt( allFen / 60);
// 小时
var hour = allHour % 24;
// 所有天
var allDay = parseInt(allHour / 24);
// 天
var day = allDay % 365;
// 年
var year = parseInt(allDay / 365)
var str = `${year}年${day}天${hour}小时${fen}分钟${miao}秒钟`;
var myBox = document.querySelector(".box");
myBox.innerHTML = str;
}
var timer = setInterval(function(){
getTime()
},1000)
Math 数学对象
Math 属性
获取 π
Math.PI
console.log(Math.PI);
Math 方法
1. Math.round(number)
四舍五入--整数
console.log(Math.round(8.5)); 值为 9
等同于 8.5.toFixed(0)
console.log(Math.round(6.4)); 值为 6
等同于 6.4.toFixed(0)
2. Math.ceil(number)
2. Math.ceil(number)
向上取整
console.log(Math.ceil(6.4)); 值为 7
console.log(Math.ceil(6.1)); 值为 7
console.log(Math.ceil(6.9)); 值为 7
3. Math.floor(number)
3. Math.floor(number)
向下取整
console.log(Math.floor(6.9)); 值为 6
console.log(Math.floor(6.1)); 值为 6
console.log(Math.floor(6.8882)); 值为 6
4. Math.random()
4. Math.random()
随机返回0.0~1.0之间的数
随机取 min~max(包含max) 之间的数字:
Math.floor(Math.random() * (max-min+1))+min;
随机取 min~max(不包含max) 之间的数字 (假设 a>b ):
Math.floor(Math.random() * (max-min))+min;
随机取 1~100 之间的数字:
Math.floor(Math.random() * (100-1+1))+1;
Math.floor(Math.random() * (100)+1;
随机取 100~1000 之间的数字:
Math.floor(Math.random() * (1000-100+1))+100;
求最大值
console.log(Math.max(1,7,6,4,9,2)); 值为 9
6. Math.min(x,y)
求最小值
console.log(Math.min(1,7,6,4,9,2)); 值为1
7. Math.pow(x,y)
// 求最大值
let arr = [3, 4, 2, 6, 8, 4, 5, 7]
let num = Math.max(...arr)
console.log(num);
// 求最小值
let arr = [3, 4, 2, 6, 8, 4, 5, 7]
let num = Math.min(...arr)
console.log(num);
7. Math.pow(x,y)
求x的y次方
console.log(Math.pow(5, 8)); // 值为 390625
console.log(5**8); // 值为 390625
8. Math.abs(number)
求 number 的绝对值
console.log(Math.abs(-10));
9. Math.sprt(number)
求 number 的平方根
console.log(Math.sqrt(81)); // 9
console.log(Math.sqrt(25)); // 5
console.log(Math.sqrt(625)); // 25

浙公网安备 33010602011771号