在Javascript中生成两个日期和时间之间的随机日期 [英] Generate random date between two dates and times in Javascript
问题描述
function generateRandomDate(start,end){
return new Date(start + Math.random ()*(end-start));
}
我用于生成随机日期的代码贴在
我想我明白你以后是什么。这将返回开始
和 end
之间的随机日期,随机小时在 startHour
和 endHour
(应该是范围 0-23
中的值)。
function randomDate(start,end,startHour,endHour){
var date = new Date(+ start + Math.random )*(结束 - 开始));
var hour = startHour + Math.random()*(endHour - startHour)| 0;
date.setHours(hour);
返回日期;
}
I want to generate a random date between two dates and between two times in javascript. For instance I want to generate a random date (between 8 am and 6 pm) between today and next tomorrow. I have tried a whole bunch of things but none of them work so I won't be pasting any code since it does not work. Has anyone done something similar
function generateRandomDate(start, end) {
return new Date(start + Math.random() * (end - start));
}
The code I am using for generating random dates is posted above
I think I understand what you are after. This will return a random date between start
and end
, with a random hour between startHour
and endHour
(which should be values in the range 0-23
).
function randomDate(start, end, startHour, endHour) {
var date = new Date(+start + Math.random() * (end - start));
var hour = startHour + Math.random() * (endHour - startHour) | 0;
date.setHours(hour);
return date;
}
这篇关于在Javascript中生成两个日期和时间之间的随机日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!