如何检查和设置当前时间是否超出特定范围[Javascript/jQuery]
我想提交一些表格,时间必须在特定的时间范围内。如果超出特定范围时间,则在特定范围时间内的新时间中生成 我的具体行程时间是工作日的08:30-17:30。 如果当前时间不在工作日,则将日期设置为星期一08:30。如何检查和设置当前时间是否超出特定范围[Javascript/jQuery],javascript,jquery,date,time,Javascript,Jquery,Date,Time,我想提交一些表格,时间必须在特定的时间范围内。如果超出特定范围时间,则在特定范围时间内的新时间中生成 我的具体行程时间是工作日的08:30-17:30。 如果当前时间不在工作日,则将日期设置为星期一08:30。 例如,如果当前时间是星期六12:00,则将其设置为星期一08:30 如果当前时间小于08:30,则将当天的时间设置为08:30。 例如,如果当前时间是星期二01:00,则将其设置为星期二08:30 如果当前时间超过17:30,则将时间设置为明天的08:30。 例如,如果当前时间是星期二2
例如,如果当前时间是星期六12:00,则将其设置为星期一08:30
如果当前时间小于08:30,则将当天的时间设置为08:30。
例如,如果当前时间是星期二01:00,则将其设置为星期二08:30
如果当前时间超过17:30,则将时间设置为明天的08:30。
例如,如果当前时间是星期二20:00,则将其设置为星期三08:30
如果当前时间是星期五,并且时间超过17:30,则将时间设置为星期一的08:30。
例如,如果当前时间是星期五20:00,则将其设置为星期一08:30
有人能帮忙吗?多谢各位
function CreatedDateFunction() {
var days = ['Sun','Mon','Tue','Wed','Thur','Fri','Sat'];
var now = new Date();
var day = days[now.getDay()]
var date = now.getDate();
var month = now.getMonth()+1;
var year = now.getFullYear();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
var month = '0'+month;
}
if(day.toString().length == 1) {
var date = '0'+date;
}
if(hour.toString().length == 1) {
var hour = '0'+hour;
}
if(minute.toString().length == 1) {
var minute = '0'+minute;
}
if(second.toString().length == 1) {
var second = '0'+second;
}
var datetime = day+' '+date+'/'+month+'/'+year+' '+hour+':'+minute;
var nowDay = day;
var nowDate = date+'/'+month+'/'+year;
var nowTime = hour+'.'+minute;
document.getElementById('idCreatedate').value= datetime;
var t = setTimeout(function(){CreatedDateFunction()},500);
sessionStorage.setItem('aDate',date);
sessionStorage.setItem('aMonth',month);
sessionStorage.setItem('aYear',year);
sessionStorage.setItem('nowDay',nowDay);
sessionStorage.setItem('nowDate',nowDate);
sessionStorage.setItem('nowTime',nowTime);
}
function checkFunction() {
var date = sessionStorage.getItem('aDate');
var month = sessionStorage.getItem('aMonth');
var year = sessionStorage.getItem('aYear');
var nowDay1 = sessionStorage.getItem('nowDay');
var nowDate1 = sessionStorage.getItem('nowDate');
var nowTime1 = sessionStorage.getItem('nowTime');
alert("Day: " + nowDay1);
alert("Date: " + nowDate1);
alert("Time: " + nowTime1);
if(nowDay1!='Sat'&&nowDay1!='Sun'){
if(nowTime1>08.30 && nowTime1<17.30){
console.log("Yes, time is in range");
//Next, plus priority date(High=2days,Medium=3days,Low=4days) and check again to be sure that it is not on Sat, Sun
}else{
console.log("no, time is out of range");
//check <8.30 or >17.30 to do next
//if(nowTime1<08.30){
//set to that day at 08.30
//}else if(nowTime1>17.30){
//set day+1 and date+1 at 08.30
//}
}
}else{
console.log("Have to set to Mon 08:30");
//nowDay1 = 'Mon';
//nowTime1 = '08.30'
}
}
函数createdDataFunction(){
var days=['Sun'、'Mon'、'Tue'、'Wed'、'Thur'、'Fri'、'Sat'];
var now=新日期();
var day=days[now.getDay()]
var date=now.getDate();
var month=now.getMonth()+1;
var year=now.getFullYear();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
if(month.toString().length==1){
变量月份='0'+月份;
}
if(day.toString().length==1){
变量日期='0'+日期;
}
if(hour.toString().length==1){
变量小时='0'+小时;
}
if(minute.toString().length==1){
变量分钟='0'+分钟;
}
if(second.toString().length==1){
变量秒='0'+秒;
}
var datetime=day+''+date+'/'+month+'/'+year+''+hour+':''+分钟;
var nowDay=天;
var nowDate=日期+'/'+月+'/'+年;
var nowTime=小时+'.'.+分钟;
document.getElementById('idCreatedate')。值=日期时间;
var t=setTimeout(函数(){createdDataFunction()},500);
sessionStorage.setItem('aDate',日期);
sessionStorage.setItem('月数');
sessionStorage.setItem('年');
sessionStorage.setItem('nowDay',nowDay);
sessionStorage.setItem('nowDate',nowDate);
setItem('nowTime',nowTime);
}
函数checkFunction(){
var date=sessionStorage.getItem('aDate');
var month=sessionStorage.getItem('aMonth');
var year=sessionStorage.getItem('aYear');
var nowDay1=sessionStorage.getItem('nowDay');
var nowDate1=sessionStorage.getItem('nowDate');
var nowTime1=sessionStorage.getItem('nowTime');
警报(“日:+nowDay1”);
警报(“日期:+nowDate1”);
警报(“时间:+nowTime1”);
if(nowDay1!='Sat'&&nowDay1!='Sun'){
如果(nowTime1>08.30&&nowTime1此函数将执行以下操作:
function checkRange(d) {
var hours = d.getHours();
var mins = d.getMinutes();
var day = d.getDay();
var dayOfWeek = 1;
if(day >= 1 && day <= 5){
if(hours < 8 || (hours === 8 && mins <= 30)){
d.setHours(8, 30, 00);
return d;
}
else if(hours > 17 || (hours === 17 && mins > 30)){
(day === 5) ? d.setDate(d.getDate() + (dayOfWeek + 7 - d.getDay()) % 7) : d.setDate(d.getDate()+1);
d.setHours(8, 30, 00);
return d;
}
}
else{
d.setDate(d.getDate() + (dayOfWeek + 7 - d.getDay()) % 7);
d.setHours(8, 30, 00);
return d;
}
return 'Time is in the range';
}
console.log(checkRange(new Date('09/12/2015 20:30:00')));
功能检查范围(d){
var hours=d.getHours();
var mins=d.getMinutes();
var day=d.getDay();
var dayOfWeek=1;
如果(第>=1天和第30天)){
(day==5)?d.setDate(d.getDate()+(dayOfWeek+7-d.getDay())%7):d.setDate(d.getDate()+1);
d、 设定时间(8,30,00);
返回d;
}
}
否则{
d、 setDate(d.getDate()+(dayOfWeek+7-d.getDay())%7);
d、 设定时间(8,30,00);
返回d;
}
返回“时间在范围内”;
}
控制台日志(检查范围(新日期('09/12/2015 20:30:00'));
请展示您尝试过的内容。您能在这篇文章中而不是仅仅在一把小提琴中发布代码吗?您能解释一下为什么我们必须像if(hours<8 | |(hours==8&&mins<30))那样检查时间,而不是像if(hours==8&&mins<30)那样检查时间吗?第一个问题是,这对您有效吗?它不完全有效。例如,console.log(checkRange(新日期('09/25/2015 20:30:00'));它将返回'2015年9月26日星期六08:30:00 GMT+0700(东南亚标准时间)'。更新条件后,您可以立即尝试。它仍然没有完成。例如,console.log(checkRange(新日期('09/25/2015 10:30:00'));它将返回'2015年9月28日星期一08:30:00 GMT+0700(东南亚标准时间)'。