0

I have a question how do I get nubmber between 2 different dates using JS. I currently have booking calendar wich works fine but I have no idea how could I make when someones clicks leave date on calendar that it would change price to days between arrival and leave date and * 45$ (let's say 45 is the price/night). https://i.stack.imgur.com/Bxqdu.jpg

Here's my code of datepicker:

      $(document).ready(function(){
      var array = <?php echo json_encode($seznamkoncni); ?>;
      
      function DisableDates(date) {
        
        
          var string = jQuery.datepicker.formatDate('dd.mm.yy', date);
          
          return [dates.indexOf(string) == -1];
          
      }
      
      
      $('#date1').datepicker({
        minDate: 0,
        changeMonth: true, 
        changeYear: true,
      beforeShowDay: function(date){
          var string = jQuery.datepicker.formatDate('dd.mm.yy', date);
          return [ array.indexOf(string) == -1 ]  
      },
      onSelect: function(dateStr) {
             var newDate = $(this).datepicker('getDate');
             if (newDate) { 
                     newDate.setDate(newDate.getDate() + 1);
             }
             $('#date2').datepicker('setDate', newDate).datepicker('option', 'minDate', newDate); 
                  }
      
      
          
      });
      
      });

$(document).ready(function(){
    var array = <?php echo json_encode($seznamkoncni); ?>; 
  
  
  
  
  function DisableDates(date) {
    
  
      var string = jQuery.datepicker.formatDate('dd.mm.yy', date);
      
      return [dates.indexOf(string) == -1];
      
  }
  
  
  $('#date2').datepicker({
    minDate: 0,
    changeMonth: true, 
    changeYear: true,
  beforeShowDay: function(date){
      var string = jQuery.datepicker.formatDate('dd.mm.yy', date);
      return [ array.indexOf(string) == -1 ]
  }
  
  
      
  });
  
  });
BlueMilkyh
  • 59
  • 1
  • 7
  • I'm confused by your question. The title asks how to calculate a date between two dates in JavaScript. Your code shows you are able to calculate a date one day after the previously selected date. The pictures simply show two calendars. There appears to be a difference in the placeholder text, but since I don't speak the language, it doesn't mean anything to me. Please show what attempts you've made in a [mre]. You can use Stack Snippets (icon looks like `<>` in the toolbar) to help. – Heretic Monkey May 24 '21 at 22:08
  • I was also confused while making question :/ I'm making booking site where you can pick arrival date and the date to leave. I have 2 datepicker calendars as you can see from the pictures and I'm trying to get number of days between arrival and leave date and multiple the number by *45$. I'm very confused beacuse I'm kinda new with jquery & JS also if you want you can help me via Discord etc... I can show you whole project over there – BlueMilkyh May 24 '21 at 22:21
  • So your question is how to calculate the number of days between two `Date` values (which is what `getDate` returns). – Heretic Monkey May 24 '21 at 22:22
  • I think so... and then multiple the number by *45$ – BlueMilkyh May 24 '21 at 22:24
  • 1
    Does this answer your question? [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript). How to multiply by 45: `var price = value * 45` – Heretic Monkey May 24 '21 at 22:25
  • that's kinda useful but I'm not sure how do I get the number between 2 date values from DatePicker... That would be helpful if you could show me – BlueMilkyh May 24 '21 at 22:28
  • Sorry, I have a paying job that takes priority. [Edit] your question to make it more clear what you mean by "the number between 2 date values". Maybe someone else can help. – Heretic Monkey May 24 '21 at 22:31
  • okay thank you anyways – BlueMilkyh May 24 '21 at 22:32
  • Not sure why they closed this and then linked to some awful ways to calculate date differences. You can literally just subtract two date objects and it will give you the difference in ms. Then just divide that by the number of ms in a day. `(new Date(to_date) - new Date(from_date)) / 86400000` Maybe you should have mention that this was specific to jQuery datepicker? That's a little more complicated than will fit in a comment. – PoorlyWrittenCode May 24 '21 at 23:24
  • Also, to help point you in the right direction, `dd.mm.yy` is not a valid date sting, Using that with `new Date()` will always return `null`. The ISO shortdate standard is `yyyy-mm-dd`. You can show your user whatever you want, but your code with have to use the correct format. https://www.w3schools.com/js/js_date_formats.asp – PoorlyWrittenCode May 24 '21 at 23:37

0 Answers0