I have two strings as follows:
var a = "11/24/2014 3:10 PM" var b = "11/23/2014 7:45 AM"
How can I compare them with JavaScript, so that I could show that the time for var b happens before var a?
DEMO
Convert the date stamp into UNIX time codes and then compare the two
var a = "11/24/2014 3:10 PM";
var b = "11/23/2014 7:45 AM";
var aDate = new Date(a).getTime();
var bDate = new Date(b).getTime();
if(aDate < bDate){
console.log('a happened before b');
}else if (aDate > bDate){
console.log('a happend after b');
}else{
console.log('a and b happened at the same time')
}
You need to Parse dates to DateType take a look at following snippet
var a = "11/24/2014 3:10 PM"
b = "11/23/2014 7:45 AM"
var aDate= new Date(Date.parse(a));
var bDate = new Date(Date.parse(b));
if (aDate> bDate ){
alert(aDate)
}else{
alert(bDate);
}
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