0

I have a function that uses a datepicker. And when choosing which day, I need to get the value of this day and use it in the filterTimes function.

To do this, I create a variable where there will be a list of all days:

let dayofweek = ['Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

But how do I now pass the value of the selected day to my function?

I'm trying though to get and test it with:

console.log(dayofweek(e.date.getDay()))

But in the end nothing comes to me in the console.

Here's what my function looks like now:

let restaurantReserve = {
    workTime: null,
    id: null,
    init: function (json, restaurantID) {
        let _self = this;
        _self.workTime = json;
        _self.id = restaurantID;

        let dayOfWeek = ['Sun','Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

        $('#reservation-date').datepicker({startDate: '+0d'}).on('changeDate', function (e) {
            const arDate = e.date.toString().split(' ');
            $('#dayofweek').val(arDate[0]);
            filterTimes();
            let input = $('[name="RestaurantReservationForm[date]"]');
            input.val(arDate[3] + '-' + (e.date.getMonth() + 1) + '-' + arDate[2]);
            _self.unSetError(input);
            $('#reservation-date .js-value').text(arDate[2] + ' ' + arDate[1]);
        });
    },

    getWorkHours: function (json, restaurant_id) {
      return json.filter(item => item.restaurant_id == restaurant_id);
    },

    getWorkHoursForDay: function (json, restaurant_id, day) {
      return getWorkHours(json, restaurant_id).filter(item => item.day === day)[0];
    },

    filterTimes: function () {
    if ((["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"].indexOf(dayOfWeek) >= 0)) {
      let workHours = getWorkHoursForDay(json, restaurantID, dayOfWeek);
      let items = document.querySelectorAll(".dropdown-menu.dropdown-menu-height-fixed li a");
      for (let item of items) {
        let itemValue = parseFloat(item.getAttribute('data-value'));
        item.parentNode.classList[((itemValue < parseFloat(workHours.open)) || (itemValue > parseFloat(workHours.close)) ? "add" : "remove")]("invisible");
      }
    }
  }
}

Initially, I had a select on the php page:

<select id="dayofweek" onchange="filterTimes()"> 
  <option value="Mon">Mon</option>
  <option value="Tue">Tue</option>
  <option value="Wed">Wed</option>
  <option value="Thu">Thu</option>
  <option value="Fri">Fri</option>
  <option value="Sat">Sat</option>
  <option value="Sun">Sun</option>
</select>

And I created a variable and took a value from it

let dayOfWeek = document.getElementById("dayofweek").value;
davy jones
  • 33
  • 5
  • 1
    Which datepicker are you using? How does it determine which day of the week a particular date falls on? – melkisadek Apr 26 '22 at 11:49
  • Hi, welcome to SO. Please do take the time to read the [tour]. In this case, there are multiple "datepickers" available with very similar initialisation code; please create a [mcve] which includes all the *relevant* includes (script src= and style link tags). – freedomn-m Apr 26 '22 at 12:04
  • Where did you add your console.log? It's not in the code provided. Maybe it's just not in the event? – freedomn-m Apr 26 '22 at 12:04
  • Accessing an array needs `[` not `(` so your code would be `console.log(dayofweek[e.date.getDay()])` your existing code for `e.date.toString().split(' ')[0]` also seems to work fine. Fiddle: https://jsfiddle.net/qb87Lgsx/ for [bootstrap-datepicker](https://stackoverflow.com/tags/bootstrap-datepicker/info) – freedomn-m Apr 26 '22 at 12:09
  • @freedomn-m Thank you, it’s clear with this, the main question is how now in the filterTimes function, by index, determine which day we have chosen? it's just that every time this number increases – davy jones Apr 26 '22 at 12:49
  • `$('#datepicker').datepicker("getDate").getDay()` will give you the current day of week, you can use this at any time, including in your `filterTimes` function - it's not clear where your current dayOfWeek variable comes from or contains, but better to store as a number 0-6 than as text. Existing answer: [get date from bootstrap datepicker](https://stackoverflow.com/a/26863454/2181514) and sample fiddle (select a date first): https://jsfiddle.net/qb87Lgsx/1/ – freedomn-m Apr 26 '22 at 12:55
  • @freedomn-m I updated the post and added to the end how this variable was originally. But now I want to get rid of that and do everything in a script Will your code help solve this problem? – davy jones Apr 26 '22 at 13:09
  • How many times are you going to delete and re-ask the same question? – 0stone0 Apr 26 '22 at 13:10

0 Answers0