0

I am trying to compare dates with different formats. I see wrong results when I compare the below, How can I converts dates to a standard format to get correct results.

Heres my Fiddle

var date1 = "4/12/2018 9:52:21 PM";
    var date2 ="4/12/2018 9:52:51 PM";
    var date3 ="2018/04/12 21:54:40";
    var dateCondition1 = (date3>date2);
    var dateCondition2 = (date2>date1);
    alert(dateCondition1); //shows wrong result
    alert(dateCondition2); //shows right result

will this be a correct comparison?

var date1 = new Date("4/12/2018 9:52:21 PM");
var date2 =new Date("4/12/2018 9:52:51 PM");
var date3 =new Date("2018/04/12 21:54:40");
var dateCondition1 = (date3>date2);
var dateCondition2 = (date2>date1);
alert(dateCondition1); //shows wrong result
alert(dateCondition2); //shows right result
Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • have a look at https://momentjs.com/, makes dealing with dates trivial – Wesley Smith Apr 12 '18 at 23:51
  • 1
    for what it's worth, you are comparing 3 strings, not dates. You'd need to parse the date strings into date objects to perform operations on them or compare them properly, all of which is again super easy with moment.js – Wesley Smith Apr 12 '18 at 23:54

2 Answers2

1

in the first block of code you are comparing strings

"4/12/2018 9:52:21 PM" > "4/12/2018 9:52:21 PM";

in the second block of code the comparison is correct,

enter image description here

Why does a boolean return in the comparison of 2 strings?

"Matt Ball"

Because, as in many programming languages, strings are compared lexicographically.

You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a through z.

ALTERNATIVE

using the library moment js is easier

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
//Currrent Date
var now = moment(),
custom = moment('Mon 03-Jul-2017, 11:00 AM', 'ddd DD-MMM-YYYY, hh:mm A');
document.write("Compare dates=>" + now.isAfter(custom));
</script>

if you want to use the three comparisons you can format the dates as you want

<html>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
     <script>
     var now   = moment();
     var date1 = moment('4/12/2018 9:52:21 PM','DD/MM/YYYY hh:mm:ss A');
     var date2 = moment('4/12/2018 9:52:51 PM','DD/MM/YYYY hh:mm:ss A');
     var date3 = moment('2018/04/12 21:54:40','YYYY/DD/MM hh:mm:ss A');
     var dateCondition1 = (date3.isAfter(date2));
     var dateCondition2 = (date2.isBefore(date1));
     alert(dateCondition1);
     alert(dateCondition2);
     </script>
</html>
1

try with pure javascript:

var date1 = new Date("4/12/2018 9:52:21 PM");
var date2 =new Date("4/12/2018 9:52:51 PM");
var date3 =new Date("2018/04/12 21:54:40");
var dateCondition1 = (date3.getTime() > date2.getTime());
var dateCondition2 = (date2.getTime() > date1.getTime());
alert(dateCondition1);
alert(dateCondition2);
aaron.xiao
  • 198
  • 8