tara-m of Underwaterpistol Code Snippets
1/30/2019 - 2:50 PM

Delivery Countdown

<p>Order in the next <span id='HMSremaining'></span> for delivery between <span id="first-delivery-date"></span> and <span id="second-delivery-date"></span></p>

      <!-- Gets current date and adds days  -->

      <script>
      // countdown time until midnight
      function calculateHMSleft()
        {
          //calculate
          var now = new Date();
          var hoursleft = 23-now.getHours();
          var minutesleft = 59-now.getMinutes();
          var secondsleft = 59-now.getSeconds();

          //format 0 prefixes
          if(minutesleft<10) minutesleft = "0"+minutesleft;
          if(secondsleft<10) secondsleft = "0"+secondsleft;

          //display
          $('#HMSremaining').html(hoursleft+"h "+minutesleft+"m "+secondsleft + "s");
        }

        calculateHMSleft();
        setInterval(calculateHMSleft, 1000);

        // get delivery date range

        // first date
        var firstDate = new Date();
        var firstNumberOfDaysToAdd = 3;
        firstDate.setDate(firstDate.getDate() + firstNumberOfDaysToAdd); 

        var dd = firstDate.getDate();
        var mm = firstDate.getMonth() + 1;
        var y = firstDate.getFullYear();

        var firstFormattedDate = dd + '/'+ mm + '/'+ y;

        // second date

        var secondDate = new Date();
        var secondNumberOfDaysToAdd = 6;
        secondDate.setDate(secondDate.getDate() + secondNumberOfDaysToAdd); 

        var dd = secondDate.getDate();
        var mm = secondDate.getMonth() + 1;
        var y = secondDate.getFullYear();

        var secondFormattedDate = dd + '/'+ mm + '/'+ y;

        $('#first-delivery-date').html(firstFormattedDate);
        $('#second-delivery-date').html(secondFormattedDate);

      </script>