简体   繁体   中英

Comparing two dates in Javascript having different formats

I have a table that fetches the date from an FTP location. In my user interface page, I have a form that will fetch all the details related to that particular FTP date. I am not able to compare the FTP dates with the one I have specified in the form.

My FTP date is in the given format "Fri Aug 02 2019 14:01:00 GMT+0530 (India Standard Time)".The app.js page has been coded to render result.js. The 15th column of my HTML table points towards the FTP date

The below code fetches the date from FTP JSON data and stores in an array(app.js).

 ftpTime.push(datadirJson1[j]['date'])

Below code is from Result.js which filter the table records according to date

 <script>
        //Function to filter the table element by ticketName
       function myFunction() {
            alert("Fetching table result")
            var input, filter, table, tr, td, i, txtValue;
            input = document.getElementById("myInput");
            filter = input.value;
            table = document.getElementById("myTable");
            tr = table.getElementsByTagName("tr");
            for (i = 0; i < tr.length; i++) {
                td = tr[i].getElementsByTagName("td")[14];
                if (td) {
                    txtValue = td.textContent || td.innerText;
                    if (txtValue.includes(filter)) {
                        tr[i].style.display = "";
                    } else {
                        tr[i].style.display = "none";
                    }
                }
            }
        }
    </script>

This is my form in result.js page which takes the user input as Date

 <form onsubmit="myFunction();return false" action="/result" method="GET">
        <div id="ticketDate">


           DATE:
            <input type="date" name="myInput" id="myInput">


            <input type="submit" name="submit" id="submit" value="submit">
        </div>
    </form>```

You can use momentjs to do that.

Install from https://momentjs.com/ Then on your comparison code, call the following:

if (td) {
  txtValue = td.textContent || td.innerText;

  if (moment(txtValue).isSame(moment(filter))) {
    tr[i].style.display = "";
  } else {
    tr[i].style.display = "none";
  }
}

If there is a problem with parsing dates with moment(), check out their documentation for further info. It is pretty simple.

you can use js Date , it can parse some format of date (yours works) and then be compared using math symbol ( + , - , < , > , ...)

 let externalDate = "Fri Aug 02 2019 14:01:00 GMT+0530 (India Standard Time)" let myFunction = () => { let myInput = document.getElementById("myInput") let extDate = new Date(externalDate) let date = new Date(myInput.value) console.log(`${extDate} is ${extDate > date ? "after" : "before" } ${date}`) }
 <form onsubmit="myFunction();return false" action="/result" method="GET"> <div id="ticketDate"> DATE: <input type="date" name="myInput" id="myInput"> <input type="submit" name="submit" id="submit" value="submit"> </div> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM