JavaScript - Date problem with date - Asked By mostafa hamdy on 14-Dec-11 09:45 AM

Hello all
I have some web page contains text box allows the user to enter the date with it , but the problem is when I try to get the returned date it becomes greater than the entered one by 2 years , which is invalid date ,the code which I wrote is as the following:
function validatedate() {
        var startDate = document.getElementById('txtDateStart');
        var endDate = document.getElementById('txtDateEnd');
        if (startDate != null && endDate != null) {
          var objstart = Date.parse(startDate.value);//I entered for start date 01/05/2011
          var start = new Date(objstart); 
          //the returned date from the new Date method is 01/05/2013
          var objend = Date.parse(endDate.value); //I entered for end date 01/06/2011
          var end = new Date(objend);
          //the returned date from the new Date method is 01/06/2013

          if (end < start) {

             alert("start date can not be greater than end date")
please if any body get what I mean and can help me please send me or tell me about some url may help me in doing that
kalpana aparnathi replied to mostafa hamdy on 14-Dec-11 01:09 PM
Try this code:

var myDt=new Date();
myDOB = document.getElementById('dateField').value.split('-');
myDate = myDOB[0];
myMonth= getMonthInDigit(myDOB[1]);
myYear = myDOB[2];
var now = new Date();
myDt.setFullYear(myYear,myMonth,myDate); //here year, month, date value should be parsed from the value u get
var diff = (now.getTime() - myDt.getTime())
if(diff < 0){
  alert("End Date should not be greater than Today");
Jitendra Faye replied to mostafa hamdy on 15-Dec-11 12:10 AM
Try like this-

var startDate = new Date($('#startDate').val());
var endDate = new Date($('#endDate').val());

if (startDate < endDate){
// Do something
  alert("start date can not be greater than end date")


Try this and let me know.