Validate days difference between two Dates in JavaScript and jQuery

Last Reply 5 days ago By pandeyism

Posted 5 days ago

How to check validation condition for date in Javascript.

I wanted to write Condition such that:

ReadDate SettlementDate Result
04-10-2019 02-10-2019 Send Flow D0071
04-10-2019 04-10-2019 Send Flow D0071
04-10-2019 23-10-2019 Send flow D0010
if(difference between read and settlement date<5(days) or read date==settlementdate)
{
    sendD0071()
}
else
{
    sendD0010()
}

How to write this in javascript

Posted 5 days ago

Hi chetan,

Refer below sample.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('[id*=btnResult]').on('click', function () {
                var d1 = new Date($('[id*=txtReadDate]').val());
                var d2 = new Date($('[id*=txtSettlementDate]').val());
                var time = d2.getTime() - d1.getTime();
                var days = time / (1000 * 3600 * 24);
                if (parseInt(days) <= parseInt(5) || d1 == d2) {
                    alert("Send Flow D0071");
                } else {
                    alert("Send Flow D0010");
                }
            });
        });
    </script>
    <input id="txtReadDate" type="text" value="10/04/2019" />
    <input id="txtSettlementDate" type="text" value="10/04/2019" />
    <input type="button" value="Result" id="btnResult" />
</body>
</html>

Demo