32

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
  • 4,128
  • 6
  • 43
  • 79
  • 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

60

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,935
  • 4
  • 28
  • 39
Igl3
  • 4,900
  • 5
  • 35
  • 69
  • 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
14

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
  • 9,031
  • 5
  • 29
  • 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
  • not working ------ Fri Aug 18 2023 11:09:09 GMT+0300>Tue Aug 22 2023 07:42:50 GMT+0300 true how can be 18.08.2023 11.09 higger then 22.08.2023 7:42 console.log(new Date(updated_at)+'>'+new Date(date), new Date(date) > new Date(updated_at)); – user2573099 Aug 22 '23 at 05:02
2
//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
  • 55,303
  • 33
  • 133
  • 164
Sanjay Nagare
  • 442
  • 2
  • 10
2

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...
}
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,413
  • 2
  • 16
  • 20
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,553
  • 10
  • 51
  • 93
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,702
  • 3
  • 32
  • 54
  • 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