Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if one DateTime is later than another in javascript

Tags:

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?

like image 209
Deepak Kumar Padhy Avatar asked Dec 02 '13 06:12

Deepak Kumar Padhy


People also ask

How do you compare two date objects in Javascript?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.


2 Answers

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

like image 107
Igle Avatar answered Sep 19 '22 00:09

Igle


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

like image 33
Prabhu Murthy Avatar answered Sep 18 '22 00:09

Prabhu Murthy