内置对象(Math,Date)
1.Math对象
Math对象不是构造函数,它具有数学常数和函数的属性和方法;
1.1常用简单方法
Math.PI //圆周率
//Math.PI--->3.141592653589793
Math.floor() // 向下取整
//Math.floor(1.9)---> 1
Math.ceil() // 向上取整
//Math.ceil(1.1)--->2
Math.round() // 四舍五入 注意负数
//Math.round(-0.5)---> -0
//Math.round(0.5)---> 1
Math.abs() // 绝对值
//Math.abs(-1)---> 1
Math.max() //求最大
//Math.max(5,6,3)---> 6
Math.min() //最小值
//Math.min(5,6,3)---> 3
封装函数—最大值和最小值
var myMath = {
Max : function(){
for(var i = 0; i < arguments.length;i++){
if(arguments[i] < arguments[i+1]){
var max = arguments[i+1];
}
}
return max;
},
Min: function(){
var min = arguments[0];
for (var J = 0 ; J < arguments.length; J++){
if(min > arguments[J+1]){
min = arguments[J];
}
}
return min;
}
}
console.log(myMath.Max(2,3,8,4,6,10,30,28,50)); // 50
console.log(myMath.Min(2,3,8,4,6,10,30,28,19)); //2
1.2随机数方法random()
Math.random() 函数返回一个浮点, 伪随机数在范围**[0,1)**,也就是说,从0(包括0)往上,但是不包括1(排除1),然后自己可以缩放到所需的范围。
//min是随机数区间最小值,max是随机数区间最大值
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandom(1,10);//随机生成1-10的整数
2.Date对象
创建一个 JavaScript Date
实例,该实例呈现时间中的某个时刻。Date
对象 即自1970年1月1日(UTC)起经过的毫秒数。
2.1Date()方法的使用
2.11获取当前时间必须实例化
var date = new Date();
console.log(date);// Mon Sep 02 2019 09:55:14 GMT+0800 (中国标准时间) {}
2.12 Date() 构造函数的参数
1.Date() 不写参数 就返回系统当前时间;
2.Date() 写参数 常用的写法
var date1 = new Date(2019,9,10);
//返回的是10月不是9月
var date2 = new Date('2019-9-10 8:8:8');
2.2格式化日期
//格式化日期 年月日 星期
var date = new Date();
console.log(date.getFullYear()); //年
console.log(date.getMonth()); //月
console.log(date.getDate()); //日
console.log(date.getDay()); //星期
// 写一个当前日期,自己尝试一下
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var arr = ['周日','周一','周二','周三','周四','周五','周六'];
var days = date.getDay();
console.log('今天是'+year+'年'+month+'月'+day+'日'+days);
//格式化时分秒
var date = new Date();
console.log(date.getHours()); // 时
console.log(date.getMinutes()); // 分
console.log(date.getSeconds()); // 秒
//打印当前 时分秒
function time() {
var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
//当时间小于10的时候,数字前边加一个0,比如09:09:09
var h1 = h < 10 ? '0' + h : h;
var m1 = m < 10 ? '0' + m : m;
var s1 = s < 10 ? '0' + s : s;
return h1 + ':' + m1 +':' + s1;
}
console.log(time());
2.3获取日期的总的毫秒数
Date 对象是基于1970年1月1日(世界标准时间)起的毫秒数;
我们经常利用总的毫秒数来计算时间,因为它更精确
//获取当前时间距离1970年1月1日总的毫秒数
// 实例化Date对象
var now = new Date();
// 1. 用于获取对象的原始值
console.log(date.valueOf())
console.log(date.getTime())
// 2. 简单写可以这么做
var now = + new Date();
console.log(now);
// 3. HTML5中提供的方法,有兼容性问题
var now = Date.now();
console.log(now);
经典倒计时案例
function countDown(time) {
// 实例化对象
var date = new Date();
//获取当前时间距离1970的毫秒数
var now = +new Date();
//目标日期 传入参数
var target = +new Date(time);
//倒计时间 转换为秒
var time = (target - now) / 1000;
//获取天
var days = parseInt(time / 3600 / 24);
//获取小时
var hours = parseInt(time / 3600 % 24);
//获取分钟
var minutes = parseInt(time / 60 % 60);
//获取秒
var seconds = parseInt(time % 60);
// 倒计时不足10的时候前边加0
var d = days < 10 ? '0' + days : days;
var h = hours < 10 ? '0' + hours : hours;
var m = minutes < 10 ? '0' + minutes : minutes;
var s = seconds < 10 ? '0' + seconds : seconds;
// 返回倒计时
console.log('倒计时为:' + d + '天' + h + '小时' + m + '分' + s + '秒') ;
}
countDown('2019-10-1 10:00:00');