3
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 进行比较 如何验证约会只能从当天开始在接下来的 7 天内修复。

我正在使用此代码但仍然无法正常工作

    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

4 回答 4

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;
}

小提琴的例子。稍作修改

于 2012-11-23T12:22:21.450 回答
1
if(serverDateTime.AddDays(7) < txtDate)
{
  //Your Code
}
于 2012-11-23T12:23:26.750 回答
0

在此处查看 JSFiddle

//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");
}
​

这只是为了指导你。你可以在这里阅读更多

于 2012-11-23T12:26:30.493 回答
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 的值。我还建议您对格式进行额外验证,以便正确理解您的日期。查看MDN Date以了解 javascript 中日期/时间的所有详细信息

编辑

我在 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")

返回“假”

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

于 2012-11-23T12:54:06.280 回答