Math对象
定义: math对象是一个静态对象(不需要实例化,可以直接用)
Math对象常用的方法:
1. min() 和 max() :取最小值和最大值
let max = Math.max(3,5,8,1);
let min = Math.min(3,5,8,1);
console.log(max); // 8
console.log(min); // 1
2.舍入方法ceil()、floor() 和round()
ceil() 方法执行向上舍入, floor() 方法执行向下舍入,round() 方法执行四舍五入。
let num = 3.14;
console.log(Math.ceil(num)); // 4
console.log(Math.floor(num)); // 3
console.log(Math.round(num)); // 3
Date对象
时间戳:从1970年到现在的秒数
常用方法:
1.获取到现在的毫秒数:
let now = Date.now();
console.log(now); // 1511767644238
let date = new Date();
date .getYear(); //获取当前年份(2位)
date .getFullYear(); //获取完整的年份(4位)
date .getMonth(); //获取当前月份(0-11,0代表1月)
date .getDate(); //获取当前日(1-31)
date .getDay(); //获取当前星期X(0-6,0代表星期天)
date .getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
date .getHours(); //获取当前小时数(0-23)
date .getMinutes(); //获取当前分钟数(0-59)
date .getSeconds(); //获取当前秒数(0-59)
date .getMilliseconds(); //获取当前毫秒数(0-999)
date .toLocaleDateString(); //获取当前日期
var mytime=date .toLocaleTimeString(); //获取当前时间
date .toLocaleString( ); //获取日期与时间
2.获得当前时间
//获取当前时间
function getNowFormatDate() {
let date = new Date();
//分隔符
let seperator1 = "-";
let seperator2 = ":";
//获取当前月份
let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
//获得当前的日
let strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
//获取时、分、秒
let hour = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + seperator2 + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + seperator2 + (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
//将日期拼接
let currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + hour;
return currentdate;
}
let nowDate = getNowFormatDate();
console.log(nowDate);