locked
How can I compare two time with current time RRS feed

  • Question

  • User932259438 posted

    hi,

    i have two time textbox:
    1. from
    2. to

    I need to compare between from and to with current time and then show alert.

    If Is my time between 8:00-15:59 show alert, if is 16:00 no show alert.

    thanks

     

    Tuesday, May 26, 2020 1:52 PM

All replies

  • User475983607 posted

    Share your code and any JavaScript libraries you are using.

    Tuesday, May 26, 2020 2:17 PM
  • User932259438 posted

    my code:

    function checkTime(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }
    
    function startTime() {
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        // add a zero in front of numbers<10
        m = checkTime(m);
        s = checkTime(s);
    
        
    
        document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
        t = setTimeout(function () {
            startTime()
        }, 500);
    }
    startTime();
    

    From this code I want to get between from and to time.

    Tuesday, May 26, 2020 2:49 PM
  • User475983607 posted

    I would write a standard DateTime to the input or hidden field or data- attribute.  Then a simple compare will work.

    @{
        ViewData["Title"] = "Index";
    }
    @{ 
        DateTime start = new DateTime(2020, 05, 26, 08, 00, 00);
        DateTime end = new DateTime(2020, 05, 26, 16, 00, 00);
    }
    <h1>Index</h1>
    <input id="start" type="text" name="start" value="@start.ToString()" />
    <input id="end" type="text" name="end"value="@end.ToString()" />
    
    <input id="Button1" type="button" value="button" />
    
    @section scripts {
    <script>
    
        $('#Button1').click(function () {
            var start = new Date($('#start').val());
            var end = new Date($('#end').val());
            var now = new Date();
    
            if (start < now & now < end) {
                console.log("Inbetween");
            } else {
                console.log("Not inbetween");
            }
        });
    
    </script>
    }

    Otherwise, look into a JavaScript library like Momentjs.

    https://momentjs.com/

    Tuesday, May 26, 2020 3:20 PM
  • User-474980206 posted

    simple:

    <body>
      <input type="time" id="fromTime"><br>
      <input type="time" id="toTime"><br>
      <button type="button" onclick="checkTimes()">check time</button>
    </body>
    
    <script>
    function checkTimes() {
       var alertRange = function(d) {
           return d >= new Date("1970/1/1 8:00") && d < new Date("1970/1/1 16:00");
       }; 
       var from = new Date("1970/1/1 " + document.getElementById('fromTime').value);
       var to = new Date("1970/1/1 " + document.getElementById('toTime').value);
    
       if (isNaN(from.valueOf())) {
           alert('invalid from data');    
       }
       if (isNaN(to.valueOf())) {
           alert('invalid to data');    
       }
    
       if (alertRange(from))
          alert("from alert");
       if (alertRange(to))
          alert("to alert");
    }
    </script>
    

    Tuesday, May 26, 2020 3:33 PM
  • User-939850651 posted

    Hi, progy85

    From my understanding, what you want are:

    1. Set from and to time textbox
    2. Update current time automatically
    3. Compare current time to see if it is between from and to time.

    If in this case , you can refer below codes:

    <script src="Scripts/jquery-3.4.1.js"></script>
    <script>
        var checkMethod;
        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
    
        function check() {
            // Get from and to time
            var from = $("#from").val();
            console.log(from);
            var to = $("#to").val();
            console.log(to);
            // Get current time
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            // add a zero in front of numbers<10
            m = checkTime(m);
            s = checkTime(s);
            
            var time = document.getElementById('time').value;
    
            if (from) {
                if (to) {
                    if (time >= from && time <= to) {
                        alert("now time is " + time + ", time between from and to");
                    } else {
                        alert("now time is " + time + ", time not between from and to");
                    }
                } else {
                    alert('to time invalid');
                }
            } else {
                alert('from time invalid');
            }
        }
    
        $(document).ready(function () {
            $("#checkBtn").on("click", function () {
                check();
            });
            startTime();
            
        });
    
        function startTime() {
    
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            // add a zero in front of numbers<10
            m = checkTime(m);
            s = checkTime(s);
    
            document.getElementById('time').value  = h + ":" + m + ":" + s;
    
            setTimeout(function () {
                startTime();
            }, 500);
        }
    </script>
    <body>
        <form id="form1" runat="server">
            <div>
                <input type="time" id="from" />
                <input type="time" id="to" />
                <input type="text" id="time" />
                <input type="button" id="checkBtn" value="Check time" />
            </div>
        </form>
    </body>

    Result:

    Hope this can help you.

    Best regards,

    Xudong Peng

    Wednesday, May 27, 2020 1:02 PM