常用的Date对象和Math对象方法

Date对象方法:

当前用户本地时间

let time = new Date();

获取整数年

console.log(time.getFullYear());

获取当前月(月份要加1)

console.log(time.getMonth()+1);

获取当前周【0-6】星期日是0

console.log(time.getDay());

获取时间日

console.log(time.getDate());

获取小时

console.log(time.getHours());

获取分钟

console.log(time.getMinutes());

获取时间秒

console.log(time.getSeconds());

获取毫秒数

console.log(time.getMilliseconds());

获取当前时间距离1970年-1-1日上午八点的毫秒差(常用于时间戳)

console.log(time.getTime());

获取时间戳的几种方式

time = Date.parse(new Date())
time = (new Date()).valueOf();
time = new Date().getTime();

 

这里顺便说下倒计时的原理以及公式

原理:

未来时间-现在时间= 剩余时间

公式:

let d = Math.floor(t/86400);
t % =86400;
let h = Math.floor(t/3600);
t % = 3600;
let m = Math.floor(t/60);
t % = 60;

 

 

Math对象常用方法:

返回一组数的最大值

Math.max()

返回一组数的最小值

Math.min()

向下取整

Math.floor()

向上取整

Math.ceil()

四舍五入,包括整数

Math.round()

返回0-1之间的随机数

Math.random

返回数字的绝对值

Math.abs()

返回数字的平方根

Math.sqrt()

 

posted @ 2018-12-16 00:07  紫诺花开  阅读(321)  评论(0编辑  收藏  举报