Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript check end date is greater than or equal to start date

Tags:

javascript

Is it possible to check whether an end date is greater than or equal to a start date in Javascript? My dates are strings in the format 'dd/mm/yyyy'.

like image 803
user517406 Avatar asked May 26 '11 08:05

user517406


People also ask

How do you validate the end date greater than the start date?

val(); var endDate = $('#end_date'). val(); if (endDate < startDate){ alert('End date should be greater than Start date. '); $('#end_date'). val(''); } });

How do you check start date is less than end date?

You can use the datepicker. getDate() method to get the currently selected date object from the input field.


5 Answers

try this

var startDate = "05/01/2011";
var endDate = "09/01/2011";
var regExp = /(\d{1,2})\/(\d{1,2})\/(\d{2,4})/;
if(parseInt(endDate.replace(regExp, "$3$2$1")) > parseInt(startDate.replace(regExp, "$3$2$1"))){
alert("greater");
}
like image 123
niksvp Avatar answered Oct 24 '22 12:10

niksvp


If the string format ('dd/mm/yyyy') doesn't change, this function should work:

function endAfterStart(start,end){
  return new Date(start.split('/').reverse().join('/')) <
          new Date(end.split('/').reverse().join('/'));
}
alert(endAfterStart('05/01/2011','09/01/2011')); //=> true

Or extend the Date.prototype:

Date.prototype.isBefore = Date.prototype.isBefore || function(dat){
   return this < dat;
}
new Date('05/01/2011'.split('/').reverse().join('/'))
   .before( new Date('09/01/2011'.split('/').reverse().join('/')) );  //=>true
like image 30
KooiInc Avatar answered Oct 24 '22 13:10

KooiInc


Most simple way to do this.

function endAfterStart(start, end) {
    var startDate = new Date(start);
    var endDate   = new Date(end);

    return endDate.getTime() >= startDate.getTime();
} 
like image 27
Richard Christensen Avatar answered Oct 24 '22 13:10

Richard Christensen


function isDate(value)
            {
                var fromDate = document.getElementById("fromDate").value
                var toDate= document.getElementById("toDate").value
                //var curr_Date= new SimpleDateFormat("dd/mm/yyyy");


            var dateRegEx = null;
            dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);

            if (dateRegEx.test(fromDate)){
            }
            else{
                alert("Invalid from date");
                return false;
            }
            dateRegEx = new RegExp(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/g);
            if(dateRegEx.test(toDate)) {
            }
            else{
                alert("Invalid to date");
                return false;
            }
            var stDate = new Date(fromDate);
            var enDate = new Date(toDate);
            var compDate = enDate - stDate;
            //var fdate=enDate-curr_Date;

            if(compDate >= 0)
                return true;
            else 
            {
                alert("To Date cannot be smaller than From Date");
                return false;
            }




            /**/
        }

This will work for Leap years also..in dd/mm/yyyy format(not any other format).

like image 40
razor Avatar answered Oct 24 '22 11:10

razor


Took me some time to find, but JQuery implements this exact functionality with DatePicker date-range. (Source code available in link as well.)

Moment.js also handles date comparisons very well using the diff function.

like image 27
Gaʀʀʏ Avatar answered Oct 24 '22 12:10

Gaʀʀʏ