0

How can I compare ISO dates in Javascript. Hour and Minute does not give correct results.

        function ConvertDateNowToISODate() {    
        var currentDate = new Date().toISOString();
        currentDate = new Date(currentDate).getTime();
        return currentDate;       
    }   
    
    function ConvertToExpirationDateToISODate(expirationDate) {
    
        var newDate = new Date(expirationDate).toISOString();
        newDate = new Date(newDate).getTime();
        return newDate;
        
    }
    
    function isFromBiggerThanTo(dtmfrom) {
        var from= ConvertToExpirationDateToISODate(dtmfrom);
        var to = ConvertDateNowToISODate();
       
        if (from > to)
            return "blue";
        else if (to < from)
            return "red";
        else
            return "grey";
}

dtmInputs= input1=2022-08-09T12:42:02.953, input2= 2022-10-08T05:00:00, input3= 2022-08-11T13:42:47.993, input4= 2022-10-08T14:35:00

my codes never drop in the red part like this. my codes never drop in the red part like this

  • Why are you converting your dates to strings? They're much easier to deal with as Date instances. – Pointy Aug 10 '22 at 14:38
  • Please create a [mre]. Use the `<>` button. –  Aug 10 '22 at 14:40
  • The first function can be just `return Date.now()`. The second function can be `return Date.parse(expirationDate)`. The code after the first *return* statement is never reached so can be deleted. Please provide example values for *dtmfrom* that demonstrate the issue. – RobG Aug 10 '22 at 20:45
  • I added the date inputs – Mehmet Ceylan Aug 11 '22 at 05:31

1 Answers1

1

from > to and to < from are both the same. Silly mistake!

Change the second condition as to > from.

Teddy
  • 4,009
  • 2
  • 33
  • 55