Math对象
Math 对象用于执行数学任务。
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。
常见的Math对象方法
- Math.random 该方法用于生成一个0~1的随机数,包括0,不包括1
// 生成一个0~1的随机数
var num1 = Math.random();
console.log(num1);
// 生成一个0~9的随机 整数
var num2 = parseInt(Math.random() * 10);
console.log(num2);
// 生成一个0到15的随机 整数
var num3 = parseInt(Math.random() * 15);
console.log(num3);
// 生成一个15~50的随机 整数
var num4 = parseInt(Math.random() * 36 + 15);
console.log(num4);
// 生成一个m~n的随机 整数
function getRandom(m, n){
return parseInt(Math.random() * (n - m +1) + m);
}
var num5 = getRandom(15,40);
console.log(num5);
- Math.abs 该方法用于获取获取一个数的绝对值
var num6 = Math.abs(-1);
var num7 = Math.abs(1);
console.log(num6); // 1
console.log(num7); //
- Math.ceil 向上取整
var num8 = Math.ceil(4.4);
var num9 = Math.ceil(4.6);
console.log(num8); // 5
console.log(num9); // 5
- Math.floor 向下取整
var num10 = Math.floor(4.4);
var num11 = Math.floor(4.6);
console.log(num10); // 4
console.log(num11); // 4
- Math.round 四舍五入
var num12 = Math.round(4.4);
var num13 = Math.round(4.6);
console.log(num12); // 4
console.log(num13); // 5
- Math.pow 用于求一个数的几次方,第一个参数是底数,第二个参数是指数
var num14 = Math.pow(2, 2);
var num15 = Math.pow(2, 3);
console.log(num14); // 4
console.log(num15); // 8
// 当只有一个参数时
var num16 = Math.pow(2);
console.log(num16); // NaN
- Math.sqrt 用来求一个数的开方
var num17 = Math.sqrt(9);
console.log(num17); // 3
- Math.max 求所有参数中的最大值
var num18 = Math.max(5, 2, -3, -4, 10);
console.log(num18); // 10
- Math.min 求所有参数中的最小值
var num19 = Math.min(5, 2, -3, -4, 10);
console.log(num19); // -4
Date对象
Date 对象用于处理日期和时间。
可以通过 new 关键词来定义 Date 对象。
常见的Date对象方法
初始化日期
- 不传递参数,当直接通过new调用Date时,并且不传递参数时,得到的时当前的系统时间
var date1 = new Date();
console.log(date1);
- 传递一个参数,Date可以接受一个字符串作为参数,要求是符合系统时间的字符串;如果传递的该参数是一个数字(例如2000),则表示2000毫秒,等于2秒,输出的结果是1970年1月1日0时0分0秒 加上 2000毫秒
var date2 = new Date("2020-8-24 08:24:02");
console.log(date2); // Mon Aug 24 2020 08:24:02 GMT+0800 (中国标准时间)
// 如果传递的该参数是一个数字(例如2000),则表示2000毫秒,等于2秒,输出的结果是1970年1月1日0时0分0秒 加上 2000毫秒
var date3 = new Date(2000);
console.log(date3); // Thu Jan 01 1970 08:00:02 GMT+0800 (中国标准时间)
- 传递两个参数,Date可以接受两个参数,第一个表示年份,第二个表示月份(0表示1月),值的范围是0 ~ 11
var date4 = new Date(2020, 7);
console.log(date4); // Sat Aug 01 2020 00:00:00 GMT+0800 (中国标准时间)
- 传递三个参数,前两个同上,第三个表示天数,这个不是从0开始,而是写几就是几号,值的范围是1 ~ 31
var date5 = new Date(2020, 7, 24); // Mon Aug 24 2020 00:00:00 GMT+0800 (中国标准时间)
console.log(date5);
// 二月份没有31天,自动计算到下一个月
var date6 = new Date(2020, 1, 31);
console.log(date6); // Mon Mar 02 2020 00:00:00 GMT+0800 (中国标准时间)
- 传递四个参数,前三个同上,第四个表示小时,值的范围是0 ~ 23
var date7 = new Date(2020, 7, 24, 8);
console.log(date7); // Mon Aug 24 2020 08:00:00 GMT+0800 (中国标准时间)
- 传递五个参数,前四个同上,第五个表示分钟,值的范围是0 ~ 59
var date8 = new Date(2020, 7, 24, 8, 24);
console.log(date8); // Mon Aug 24 2020 08:24:00 GMT+0800 (中国标准时间)
// 遇到60进1
var date9 = new Date(2020, 7, 24, 8, 60);
console.log(date9); // Mon Aug 24 2020 09:00:00 GMT+0800 (中国标准时间)
- 传递六个参数,前五个同上 第六个表示秒,值的范围是0 ~ 59
var date10 = new Date(2020, 7, 24, 8, 24, 30);
console.log(date10); // Mon Aug 24 2020 08:24:30 GMT+0800 (中国标准时间)
获取日期部分内容
var nowTime = new Date();
console.log(nowTime);
- 获取年 getFullYear()
var Y = nowTime.getFullYear();
console.log(Y);
- 获取月 getMonth()
var M = nowTime.getMonth();
console.log(M);
- 获取天数 getDate()
var D = nowTime.getDate();
console.log(D);
- 获取小时 getHours()
var H = nowTime.getHours();
console.log(H);
- 获取分钟 getMinutes()
var Minu = nowTime.getMinutes();
console.log(Minu);
- 获取秒数 getSeconds()
var S = nowTime.getSeconds();
console.log(S);
- 获取毫秒值 getMillionseconds()
var Mill = nowTime.getMilliseconds();
console.log(Mill);
- 获取星期几 getDay() , 注意:0 是星期天,周一到周六 1 ~ 6
var Day = nowTime.getDay();
console.log(Day);
设置日期
var theTime = new Date("2020-8-24 08:24:02");
console.log(theTime); // Mon Aug 24 2020 08:24:02 GMT+0800 (中国标准时间)
- 设置年 setFullYear()
var setY = theTime.setFullYear(2014);
console.log(theTime); // Sun Aug 24 2014 08:24:02 GMT+0800 (中国标准时间)
- 设置月 setMonth()
var setM = theTime.setMonth(1);
console.log(theTime); // Mon Feb 24 2014 08:24:02 GMT+0800 (中国标准时间)
- 设置天 setDate()
var setD = theTime.setDate(14);
console.log(theTime); // Fri Feb 14 2014 08:24:02 GMT+0800 (中国标准时间)
- 设置小时 setHours()
var setH = theTime.setHours(20);
console.log(theTime); // Fri Feb 14 2014 20:24:02 GMT+0800 (中国标准时间)
- 设置分钟 setMinutes()
var setMinu = theTime.setMinutes(30);
console.log(theTime); // Fri Feb 14 2014 20:30:02 GMT+0800 (中国标准时间)
- 设置秒数 setSeconds()
var setS = theTime.setSeconds(24);
console.log(theTime); // Fri Feb 14 2014 20:30:24 GMT+0800 (中国标准时间)
以上是常见的一些方法,如果想要了解更多可以参考:
JavaScript Math对象参考手册
JavaScript Date对象参考手册