admin管理员组

文章数量:1323524

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

I'm getting serverDateTime on master page load and access that variable in all pages and paring serverDateTime with txtDate.value how do I validate Appointment can be fixed for next seven days only from current day.

I M USING THIS CODE BUT STILL NOT WORKING

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

I'm getting serverDateTime on master page load and access that variable in all pages and paring serverDateTime with txtDate.value how do I validate Appointment can be fixed for next seven days only from current day.

I M USING THIS CODE BUT STILL NOT WORKING

    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;
                }
            }       
Share Improve this question edited Nov 23, 2012 at 12:53 lax asked Nov 23, 2012 at 12:13 laxlax 5182 gold badges11 silver badges26 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

This should work:

//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.

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

Before you can pare dates, they must be dates... In your example they are both strings so try something like this :

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

Try this and change the values of txtDate.value. I also remend that you do extra verification on the formats in order for your dates to be correctly understood. Have a look on MDN Date for all the details of date/time in javascript

EDIT

I tried this exactly in the Chrome (v23.xxx) console :

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

Then I tried this :

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

which returns "true" and tried this :

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

which returns "false"

It looks like this answers your question and further issues are nothing to do with the algo but more like how your browser interprets it and how you manage it.

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

This is just to guide you. You can read more here

本文标签: javascriptHow to check if a date falls within the next 7 daysStack Overflow