var d = new Date(i)
if(d.getDay() >= 1 &&d.getDay() <= 5){
count++
}
}
getRandomDateBetween() { // 生成当前时间一个月内的随机时间。var date = new Date()
var e = date.getTime()//当前时间的秒数
var f = date.getTime()-(30*24*60*60*1000)//30天之前的秒数,
//这里是注释打印不用看 console.log(a.Format('yyyy-MM-dd hh:mm:ss'),b.Format('yyyy-MM-dd hh:mm:ss'))
//这里是注释打印不用看console.log(new Date(this.RandomNumBoth(f,e)).Format('yyyy-MM-dd hh:mm:ss'))
return new Date(Store.RandomNumBoth(f,e)).Format('yyyy-MM-dd hh:mm:ss')
// Store.RandomNumBoth(当前时间秒数,30天之前的时间秒数),我这是react语法在这调用加类名(Store),你们是别的就直接 this,RandomNumBoth
},
RandomNumBoth(Min,Max){
var Range = Max - Min
var Rand = Math.random()
var num = Min + Math.round(Rand * Range)//四舍五入
return num
},
最近需要写个小功能,就把这个功能拆成小方法了,觉得日期这玩意儿基本上都能用到,就记录下来,方便自己以后查找。
共有如下几种情况:
1、开始日期小于结束日期
2、两个日期相等,天数为0,之间没有日期
3、开始日期小于结束日期,天数为0,之间没有日期
<b>PS</b>:唯一的坑就是new Date()月份的参数介于 0 到 11 之间。也就是说,如果希望把月设置为 8 月,则参数应该是 7。
getTime() :返回从 1970 年 1 月 1 日至今的毫秒数。
getDate():返回日期,一个月中的某一天
setDate():设置日期,一个月中的某一天