26

Through the form i am getting two values like

   Start datetime = '01/12/2013 12:00:00 AM' and
   End datetime = '02/12/2013 12:00:00 AM'.

How I can validate the start datetime must be less than end datetime in javascript?

Deepak Kumar Padhy
  • 3,728
  • 4
  • 38
  • 75
  • Possible duplicate of [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Heretic Monkey Oct 03 '18 at 12:42

8 Answers8

50

Asuming you received a date in Javascript Date format you need Date.parse() function or compare by comparison operators. It will return the milliseconds that have passed since 01/01/1970 00:00

Somehow like this:

if(Date.parse(datetimeStart) < Date.parse(datetimeEnd)){
   //start is less than End
}else{
   //end is less than start
}

Here is a Fiddle

gokhanakkurt
  • 4,895
  • 4
  • 28
  • 39
Igle
  • 4,572
  • 4
  • 30
  • 64
  • 1
    This assumes that the Date.parse can correctly handle the string it is given. For non US dates or non-ISO dates or user supplied dates, Date.parse can fail to recognise the date delimiters or the date format, so confuse 2/3/21 as 3rd of February (US format) or 2nd Feb (UK format). Either validate and convert to a known format (ie: convert to US/ISO) or use a library like moment.js to handle formatting/conversion. – andora Jun 17 '21 at 16:29
10

its really simple in javascript

var startTime = new Date('01/12/2013 12:00:00 AM');
var endTime = new Date('02/12/2013 12:00:00 AM');

and then all you need to do is compare

if( startTime < endTime){
   alert("start time is lesser");
}

More on this here

Community
  • 1
  • 1
Prabhu Murthy
  • 8,884
  • 5
  • 28
  • 36
  • This assumes that the Date() can correctly handle the string it is given. For non US dates or non-ISO dates or user supplied dates, Date() can fail to recognise the date delimiters or the date format, so confuse 2/3/21 as 3rd of February (US format) or 2nd Feb (UK format). Either validate and convert to a known format (ie: convert to US/ISO) or use a library like moment.js to handle formatting/conversion. – andora Jun 17 '21 at 16:30
1
//StartDate & EndDate two dates

if (StartDate < EndDate)
   // code

if you just want the dates, and not the time

if (StartDate.Date < EndDate.Date)
    // code
Praveen
  • 53,079
  • 32
  • 129
  • 156
Sanjay Nagare
  • 422
  • 2
  • 10
1

Try this following code:

function dateCheck() {
    var fDate = new Date("26/05/2013");
    var lDate = new Date("24/05/2013");
    if(fDate <= lDate) {
        alert("true");
        return true;
    }
    alert("false");
    return false;
}
Mr.G
  • 3,253
  • 2
  • 15
  • 20
1

If you want to compare only dates then this will work.

var dt1 = new Date('12/31/2013');
var dt2 = new Date('12/31/2014');
var dt1obj = Date.parse(dt1);
var dt2obj = Date.parse(dt2);
if(dt2obj <= dt1obj){
//your code here...
}else{
//your code here...
}
0
var record_day1=fromDate.split("/");
    var sum1=record_day1[1]+'/'+record_day1[0]+'/'+record_day1[2];  
    var record_day2=toDate.split("/");
    var sum2=record_day2[1]+'/'+record_day2[0]+'/'+record_day2[2];  
    var record1 = new Date(sum1);
    var record2 = new Date(sum2); 
    if(record2 < record1)
    {
            alert("End date must be greater than start date");
            return false;
    }  

Here we are splitting the date and then combining it for comparing it hope it will work thanks.....:)

Just code
  • 13,105
  • 10
  • 49
  • 89
0

use the date object

    Date1 = new Date('01/12/2013 12:00:00 AM');
    Date2 = new Date('02/12/2013 12:00:00 AM');
    Date1-Date2//in millisecond
0

You can use Date.parse as following

if (Date.parse(datetimeStart) < Date.parse(datetimeEnd)) {} else {}
Ali
  • 2,508
  • 3
  • 28
  • 47
  • This assumes that the Date.parse can correctly handle the string it is given. For non US dates or non-ISO dates or user supplied dates, Date.parse can fail to recognise the date delimiters or the date format, so confuse 2/3/21 as 3rd of February (US format) or 2nd Feb (UK format). Either validate and convert to a known format (ie: convert to US/ISO) or use a library like moment.js to handle formatting/conversion. – andora Jun 17 '21 at 16:31