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?
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.
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
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"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With