1

i have two input box for getting date value in the format "dd/mm/yyyy".

i have to create coupon for a shop, so i have to check 1st date should be from tomorrow. for ex. if today is 5th sep, then 1st date should not be before 6th sep.

and 2nd date should be atleast 1day greater than the 1st date. if user entered 1st date as 10th sep, then 2nd date should not be same or before 10th sep.

so if a user enters 31st jan 2013 ( 31/01/2013) as 1st date then 2nd date can be 1st feb or any date after 1st feb. so i hvae to check the date validation also. for date validation i am using the following code -

    function chkdate(y,m,d)
    {
        var date = new Date(y,m-1,d);
        month1 = date.getMonth()+1;
        date1  =date.getDate();
        if(month1 <10)
              { 
               month1 = "0"+month1;
              }
        if(date1 <10)
              { 
               date1 = "0"+date1;
              }
        var convertedDate =""+date.getFullYear() + (month1) + date1;
        var givenDate = "" + y + m + d;
        return ((givenDate==convertedDate)?true:false); 
     }  

i am callling above function inside a function -

  function generate_coupon()
  {
         var f_arr = from_date.split("/"); //from_date is from 1st input date value.
     var f_day = f_arr[0];
     var f_month =f_arr[1];
     var f_year =f_arr[2]; 

         var t_arr = to_date.split("/"); //to_date is from 2nd input date value.
     var t_day = t_arr[0];
     var t_month =t_arr[1];
     var t_year =t_arr[2]; 

         if (chkdate(f_year,f_month,f_day)== true && chkdate(t_year,t_month,t_day)== true)
         {

         }
        else
         {
          alert('Enter Valid Date - dd/mm/yyyy');
         }
    }

in that if condition i have to check both date values - the 1st date value is from tomorrow or not and 2nd date value should differ from 1st date by atleast 1 day. if both conditions are true then i wil generate a coupon or else i will alert invalid date.

how should i do it ??

1
  • You should have a look at moment.js or datejs for this. Date operations can be really nasty^^
    – Andreas
    Sep 5, 2012 at 6:13

4 Answers 4

0

rather doing this type of validation, i think it will be more easy for you that you fill the other date field you self using some defined code. and made those fields UN-editable

1
  • yes. that type also we can do. but the above is the 1st priority the client needs. if i wil not get any solution then i have to do like this. Sep 5, 2012 at 6:05
0

Farid has made a good point, however if your base date is dynamic something like this should work:

function isValidInput(date1, date2) {
    return date2 >= getNextDate(date1);
}

function getNextDate(date) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    var date_ms = date.getTime();
    var nextDate_ms = date_ms + ONE_DAY;

    var nextDate = new Date(nextDate_ms);

    // Make sure to get rid of time, so that it's always at 00:00:00 hour.
    return new Date(nextDate.getYear(), nextDate.getMonth(), nextDate.getYear());
}
0
function checkDates(d1, d2) {
    if (d1 instanceof Date && d2 instanceof Date) {
        var today = new Date();
        today.setHours(0, 0, 0, 0);
        if (date1.getTime() < (today.getTime() + 86400000)) {
            return "First date should be after today";
        }
        if (date2.getTime() < (date1.getTime() + 86400000)) {
            return "Second date should be after First date";
        }
        return "Dates are valid";
    }
    return "One or more invalid date";
}



var date1Str = "6/9/2012";
var date2Str = "7/9/2012";

var date1Arr = date1Str.split("/");
var date2Arr = date2Str.split("/");

var date1 = new Date(date1Arr[2], date1Arr[1] - 1, date1Arr[0]);
var date2 = new Date(date2Arr[2], date2Arr[1] - 1, date2Arr[0]);

alert(checkDates(date1, date2));
0

Try this

function generate_coupon() {
    var from_date = document.getElementById("fromDate");
    var to_date = document.getElementById("toDate");

    var from = new Date(from_date.value);
    var to = new Date(to_date.value);    
    var today = new Date();
    var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    if (from >= tomorrow && to > from) {

    } else {
        alert('Enter Valid Date - dd/mm/yyyy');
    }
}​

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.