0

I wanted to know if the method of comparing two string values of dates, mentioned in the title, is legit. I have tried multiple different versions of comparison and they all seem to work.

console.log("23.3.2018" > "24.3.2018")
//VM16380:1 false
//undefined
console.log("23.3.2018" < "24.3.2018")
//VM16381:1 true
//undefined
console.log("24.3.2017" < "24.3.2018")
//VM16384:1 true
//undefined
console.log("24.3.2018" < "20.3.2017")
//VM16385:1 false

Thank you!

Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28
Vid
  • 163
  • 1
  • 13
  • 1
    `console.log( "23.12.2018" > "23.8.2018" )` -> `false`. So no, it does not work unless you use ISO compliant strings. – Sirko Aug 06 '18 at 08:20
  • Then what exactly is being compared in those cases? – Vid Aug 06 '18 at 08:26
  • The string values are compared lexicographically. So, e.g., `"A" < "B" < "C"`etc. – Sirko Aug 06 '18 at 08:27
  • So `console.log( "23.12.2018" > "23.8.2018" )` is `false` because `8 > 1`? – Vid Aug 06 '18 at 08:30
  • In this case, yes. Note, that also the dots are included in the comparison. So in `"23.12.2018" > "23.1.2018"` the deciding characters are `"2" > "."`, which in this case is correct, but in general might not be. If you want to compare date/time strings, use the ISO format. – Sirko Aug 06 '18 at 08:45

2 Answers2

1

You can parse your String date to Date Object and compare :

function CompareDate(dateStr1,dateStr2) {
       var dateArry1 = dateStr1.split(".");
        var dateArry2 = dateStr2.split(".");
        //JavaScript counts months from 0 index so we have to do -1:January - 0, February - 1, and so on..... 
       var dateOne = new Date(dateArry1[2], dateArry1[1]-1, dateArry1[0]); //Year, Month, Date
      var dateTwo = new Date(dateArry2[2], dateArry2[1]-1, dateArry2[0]); //Year, Month, Date
      if (dateOne > dateTwo) {
          console.log("Date One is greather then Date Two.");
          return true;
       }else if(dateOne < dateTwo) {
         console.log("Date Two is greather then Date One.");
         return false;
      }else if(dateOne.toDateString() === dateTwo.toDateString()) {
         console.log("Date are same.");
         return false;
      }
      return false;
 }

console.log(CompareDate("23.3.2018","24.3.2018"));
//VM16380:1 false
//undefined
console.log(CompareDate("23.3.2018","24.3.2018"));
//VM16381:1 true
//undefined
console.log(CompareDate("24.3.2017", "24.3.2018"));
//VM16384:1 true
//undefined

console.log(CompareDate("24.03.2018" , "20.3.2017"));
console.log(CompareDate("24.3.2018" , "24.03.2018"));
//VM16385:1 false

WARNING !

In some browsers, months or days with no leading zeroes may produce an error:

var d = new Date("2015-3-25");

So better to prepend zero in month and days in case of length is 1.

Community
  • 1
  • 1
NullPointer
  • 7,094
  • 5
  • 27
  • 41
0

you can compare the date values in javascript like this :

var start= new Date('2018.3.23');
var end= new Date('2018.3.24');
 if (start < end) 
 {
  console.log(true);
 } 
AlexiAmni
  • 382
  • 1
  • 15
  • 1
    Please do not recommend the built-in parser, particularly for non–standard strings. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) `new Date('2018.3.23')` returns an invalid date in at least one commonly used browser. – RobG Aug 06 '18 at 09:05