Search code examples
javascriptjquerybootstrap-4moment-timezone

Change variable by firing JavaScript date event


Hi I am working on a project where client wants to update a variable at 12:00 PM

Technologies:

  1. CodeIgniter 3
  2. Bootstrap 4 js
  3. JQuery UI Any help will be appreciated!

Solution

  • here is a basic guide of how this can be done...

    // Set the next delivery date
    var NextDelivery = null;
    
    function CalcTimeOut(){
       var cutOffTime = new Date();
       var currentTime = new Date();
       var difference = null;
    
       if(cutOffTime.getHours() < 12) {
          // If we are before 12 we can deliver today
           cutOffTime.setHours(12,0,0,0);
          NextDelivery = new Date();
       } else {
          // Set new cutoff time
          cutOffTime.setHours(36,0,0,0);
          // If we are after 12 we can only deliver tomorrow.
          var currentDate = new Date();
          currentDate.setDate(currentDate.getDate() + 1);
          currentDate.setHours(0,0,0,0);
          NextDelivery = currentDate;
       }
       var difference = (cutOffTime.getTime() - currentTime.getTime());
       // Recalculate if we need to...
       setTimeout(function(){
          CalcTimeOut();
       }, difference);
    }
    
    // Call from where ever you need to.
    CalcTimeOut();