获取传入时间前n天的时间
new Date() 获取当前时间
getTime() 获取时间戳
getFullYear() 获取时间的年份
getMonth()获取时间的月份,从0到11,所以与实际相比需加1
getDate()获取时间是几号
getHours()获取时间的小时
getMinutes()获取时间的分钟
getSeconds()获取时间的秒
/**
* 获取传入时间前n天的时间
* time 传入时间 默认当前时间
* one 前几天
* */
function theDayBefore (time, one) {
const date = time ? time : new Date();
const day = one ? one : 1;
const dateBefore = new Date(date.getTime() - day * 24 * 60 * 60 * 1000);
let y = dateBefore.getFullYear();
let m = dateBefore.getMonth() + 1;
let d = dateBefore.getDate();
let hh = dateBefore.getHours();
let mm = dateBefore.getMinutes();
let ss = dateBefore.getSeconds();
return y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss;
}
生成[0,max]到任意数的随机数,公式如下:
// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));