Math对象(数学对象)JavaScript内置对象,无需创建,直接存在
1.Math对象下面的属性和方法
Math.PI : 表示π
console.log(Math.PI);
Math.pow() : 求幂
console.log(Math.pow(2, 4));
Math.random() : 随机数
console.log(Math.random());
Math.ceil() : 向上取整
console.log(Math.ceil(2.000009));
Math.floor() :向下取整
console.log(Math.floor(2.9999999));
Math.round() : 四舍五入取整
console.log(Math.round(3.56));
Math.abs() : 绝对值
console.log(Math.abs(-100));
Math.sqrt() : 开根号
console.log(Math.sqrt(4));
Math.max() : 求最大值
console.log(Math.max(23, 567, 23, 1234, 567));
Math.min() : 求最小值
console.log(Math.min(23, 567, 23, 1234, 567));
Date对象(日期对象)
1.创建日期对象-构造函数或者类
var d=new Date --d:为实例对象, Date:为构造函数或类、
日期对象的方法:
var d = new Date();
getYear() 获取当前离1900年的年份差
getFullYear() 获取年份
getMonth() 获取月份0-11表示 1月-12月
getDate() 获取日期
getDay() 获取星期 0-6表示星期日-星期六
getHours() 获取时
getMinutes() 获取分
getSeconds() 获取秒
getTime() 获取1970.1.1离当前的时间的毫秒数。(unix系统)
三种设置日期方法
1.字符串设置-(值不会自动进位)
var date = new Date('2020-10-1 12:12:12');
// var date = new Date('2020/10/1 12:12:12');
// var date = new Date('2020.10.1 12:12:12');
// var date = new Date('2020\10\1 12:12:12'); //不成立 \斜杠代表转义字符。 例如:\n换行
console.log(date.toLocaleString()); //2020/10/1 下午12:12:12
2.数字设置-(值会自动进位)
var date = new Date(2020, 9, 1, 12, 13, 14);
console.log(date.toLocaleString());//2020/10/1 下午12:13:14 设置月份自动+1
var date = new Date(2020, 13, 1, 12, 13, 14);
console.log(date.toLocaleString()); //2021/2/1 下午12:13:14
3.设置日期 - 通过日期对象下面的方法。--自动进位(get - > set)
var date = new Date();
date.setDate(date.getDate() + 10); //获取今天的日期+10天,赋值回去
console.log(date.toLocaleString()); //2020/7/9 下午3:37:30
date.setMonth(date.getMonth() + 10); //设置月份,不需要获+1,设置的时候自动抵消。
console.log(date.toLocaleString()); //2021/5/29 下午3:39:02