如何检查日期是否在接下来的7天内

时间:2012-11-23 12:13:03

标签: javascript asp.net

function validateDate() {               
    serverDateTime="2012-11-23 17:06:46";
    txtDate.value="30-Nov-2012"

    if (how i check here){
        alert("Appointment can be fixed for next seven days only from current day.");                       
        return false;
    }else{
        return true;
    }
}

我在主页加载时获取serverDateTime并在所有页面中访问该变量,并将serverDateTime与txtDate.value进行比较。如何验证约会可以在当天只修复接下来的七天。

我使用这个代码但仍然没有工作

    function validateDate()
            {           
                var dateDifference;
                pickedDate = Date.parse(txtDate.value.replace(/-/g, " "));
                todaysDate = new Date(serverDateTime);
                todaysDate.setHours(0, 0, 0, 0);
                dateDifference = Math.abs(Number(todaysDate) - pickedDate);
                //7 Days=604800000ms
                if (dateDifference > 604800000)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }       

4 个答案:

答案 0 :(得分:2)

这应该有效:

//Get the date value of next week.
var today = new Date(serverDateTime);
var nextWeek = Date.parse(new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7));

//If nextWeek is smaller (earlier) than the value of the input date, alert...
if (nextWeek < Date.parse(txtDate.value)){
    alert("Appointment can be fixed for next seven days only from current day.");                       
    return false;
}else{
    return true;
}

Fiddle example. Slightly modified

答案 1 :(得分:1)

if(serverDateTime.AddDays(7) < txtDate)
{
  //Your Code
}

答案 2 :(得分:0)

See JSFiddle here

//Set 1 day in milliseconds
var one_day=1000*60*60*24;
var serverDateTime= new Date("2012-11-23 17:06:46");
var newDate = new Date("30-Nov-2012"); //replace with txtDate.value

//Calculate difference btw the two dates, and convert to days
var diff = Math.ceil((newDate.getTime() - serverDateTime.getTime())/one_day);
if(diff >= 7)
{
    alert("7 days");   
}
else
{
    alert("Not up to 7 days");
}
​

这只是为了指导你。你可以read more here

答案 3 :(得分:0)

在比较日期之前,它们必须是日期...... 在你的例子中,它们都是字符串,所以尝试这样的事情:

function validateDate() {  
  var one_day=1000*60*60*24; 
  serverDateTime="2012-11-23 17:06:46";
  txtDate.value="30-Nov-2012"

  var _dtSvr = Date.parse(serverDateTime);
  var _dtTxt = Date.parse(txtDate.value);

  if (((_dtTxt - _dtSvr)/one_day) > 7){
    alert("Appointment can be fixed for next seven days only from current day.");                       
    return false;
  }else{
    return true;
  }
}

尝试此操作并更改txtDate.value的值。 我还建议您对格式进行额外验证,以便正确理解日期。 有关javascript中日期/时间的所有详细信息,请查看MDN Date

修改

我在Chrome(v23.xxx)控制台中完全尝试了这一点:

function validateDate(serverDateTime, txtDate)
        {           
            var dateDifference;
            pickedDate = Date.parse(txtDate.replace(/-/g, " "));
            todaysDate = new Date(serverDateTime);
            todaysDate.setHours(0, 0, 0, 0);
            dateDifference = Math.abs(Number(todaysDate) - pickedDate);
            //7 Days=604800000ms
            if (dateDifference > 604800000)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

然后我尝试了这个:

validateDate("2012-11-23 17:06:46", "30-Nov-2012")

返回“true” 并尝试了这个:

validateDate("2012-11-23 17:06:46", "30-Dec-2012")

返回“false”

看起来这回答了你的问题,进一步的问题与算法无关,但更像是你的浏览器如何解释它以及你如何管理它。