内置对象就是js语言自带的一些对象,这些对象供开发者使用,并提供了一些常用的或是最基本而必要的功能(属性和方法)。内置对象的最大优点就是帮助我们快速开发。js提供了多个内置对象:Math Data Array String等。
自己的理解,它是一种已经写好了的存储在内部的一种对象,可以直接拿来使用。
如何学习对象中的方法:
内置对象的使用例子:
console.log(Math.PI); //3.1415926...
console.log(max(1,2,3)); //3
console.log(max(1,2,'pink老师')); //NaN
console.log(max()); //-Infinity
包装自己的数学对象:
var Mymath = {
PI : 3.141592653,
max : function() {
var max = arguments[0];
for (var i = 0; i < arguments[i]; i++) {
if(arguments[i] > max) {
max = arguments[i];
}
}
return max;
},
min : function() {
var min = arguments[0];
for (var i = 0; i < arguments[i]; i++) {
if(arguments[i] < min) {
min = arguments[i];
}
}
return min;
}
}
console.log(Mymath.max(1,2,3));
console.log(Mymath.PI);
Math绝对值和三个取整方法:
绝对值:
console.log(Math.abs(1)); //1
console.log(Math.abs(-1)); //1
console.log(Math.abs('-1')); //1 隐式转换,会把字符型'-1'转换为数字型1
三个取整:
向下取整(往小了取):floor
console.log(Math.floor(1.9)); //1
console.log(Math.floor(1.1)); //1
向上取整(往大了取):ceil
console.log(Math.ceil(1.1)); //2
console.log(Math.ceil(1.9)); //2
四舍五入:round 四舍五入.5的特殊之处 往大了取。在负数里特别注意,-1.5的四舍五入为-1。
console.log(Math.round(1.5)); //2
console.log(Math.round(1.4)); //1
console.log(Math.round(-1.5)); //-1
console.log(Math.round(-1.4)); //-1
随机数random:返回一个随机的小数,位于区间[0,1)。
这个方法里不跟参数,
代码验证:
console.log(Math.random());
如果我们想要得到两个数之间的随机整数,并且包含这两个整数 :
function getRandom(min,max) {
return Math.floor(Math.random() * (max - min +1)) + min;
}
console.log(getRandom(1,10)); //包括1和10
//随机点名
var arr = ['张三' ,'张三丰','李四','李思思'];
console.log(arr[getRandom(0,3)]);
date的使用
1.如果没有参数,则返回当前系统的当前时间。
var date = new Date();
console.log(date);
2.参数常用的写法
数字型 2022,02,28 字符型 '2022-02-28 19:04:33'
var date1 = new Date(2022,02,28); //返回的是3月不是2月
console.log(date1);
var date2 = new Date('2022-02-28 19:04:33');//('2022/2/28 19:04:33') 这样的写法也可
console.log(date2);
Date对象和Math对象不一样,date对象需要实例化后才能使用。
Date对象用来处理日期和时间。
获取当前时间必须实例化。括号内有时间就显示括号内的时间。
日期获取方法 年月日星期
var date = new Date();
console.log(date.getFullYear()); //获取当前日期的年 2022
console.log(date.getMonth() + 1); //获取当前日期的月 记得+1,月份由0-11。 2
console.log(date.getDate()); //获取日 28
console.log(date.getDay()); //获取周几 当日的星期 周日返回0,周一到周六返回1到6 1
通过上边的式子我们可以拼接一个完整的日期如下:
var year = date.getFullYear(); //获取当前日期的年
var month = date.getMonth() + 1; //获取当前日期的月 记得+1,月份由0-11。
var day = date.getDate(); //获取日
var xq = date.getDay(); //由于这里返回的是数字,而我们一般使用的是汉字,所以采用数组,取这里的数字作为索引号
var arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
console.log('今天是:' + year + '年' + month + '月' + day + '日' + arr[xq]);
获取 当前时分秒
var date = new Date();
console.log(date.getHours()); //时
console.log(date.getMinutes()); //分
console.log(date.getSeconds()); //秒
//编写函数,得到当前的时分秒
function getNow() {
var h = date.getHours();
h = h < 10 ? '0' + h: h;
var m = date.getMinutes();
m = m < 10 ? '0' + m: m;
var s = date.getSeconds();
s = s < 10 ? '0' + s: s;
return h + ':' + m + ':' + s;
}
console.log(getNow());
获取1970.1.1到现在过了多少毫秒数。(时间戳)
//1.valueOf() getTime()
var date = new Date();
console.log(date.valueOf());
console.log(date.getTime());
//2. +new Date();
var date1 = +new Date();
console.log(date1);
//3.now()
console.log(Date.now());
倒计时案例 (重点)
function countDown(time) {
var nowTime = +new Date();
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('2022-3-6 8:0:0'));