0

I have two arrays of dates. The first one have all the booked dates and the second one will have dates between "start day" and "end day" which user will pick.

Now I have to confirm that the days between the start and stop will not be found from the fully booked dates array.

I'm using Vue.js to update data.

Here is what I have done to get those dates:

          /**
           * Get dates from datepicker and format it to the same format that fully booked array has it's dates.
          **/                
            var day1 = moment( this.startDate, 'DD/MM/Y').format('Y,M,D');
            var day2 = moment( this.endDate, 'DD/MM/Y').format('Y,M,D');
            var start = new Date(day1);
            var end = new Date(day2);

          /**
           * Get dates between start and stop and return them in the dateArray.
          **/ 
            Date.prototype.addDays = function(days) {
              var dat = new Date(this.valueOf());
              dat.setDate(dat.getDate() + days);
              return dat;
            };

            function getDates(startDate, stopDate) {
              var dateArray = [];
              var currentDate = startDate;
                while (currentDate <= stopDate) {
                  dateArray.push(currentDate);
                  currentDate = currentDate.addDays(1);
                }
              return dateArray;
            }

            var dateArray = getDates(start, end);

          /**
           * Set dateArray in to Vue.js data.
          **/ 
            this.$set('daysBetweenStartStop', dateArray);

          /**
           * Get arrays of dates from the Vue.js data. calendar = Booked dates | matchingDays = Dates between start and stop.
          **/ 
            var calendar = this.fullDates;
            var matchingDays = this.daysBetweenStartStop;

          /**
            * @description determine if an array contains one or more items from another array.
            * @param {array} haystack the array to search.
            * @param {array} arr the array providing items to check for in the haystack.
            * @return {boolean} true|false if haystack contains at least one item from arr.
          */
            var findIfMatch = function (haystack, arr) {
                return arr.some(function (v) {
                    return haystack.indexOf(v) >= 0;
                });
            };
            var matching = findIfMatch(calendar, matchingDays);

          /**
           * Check the results. If false we are good to go.
          **/ 
            if (matching){
              alert('WE FOUND A MATCH');
            } else {
              alert('GOOD TO GO');
            }

Arrays are in the following format:

var calendar = [
Sun Oct 02 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 09 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 16 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 23 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST)
]

var matchingDays = [
Fri Oct 28 2016 00:00:00 GMT+0300 (EEST),
Sat Oct 29 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST)
]

My problem is that even if those two arrays have exactly same dates they will still somehow be considered as a not identical. Any ideas how to get this working?

8
  • are the arrays always sorted ? Commented Sep 30, 2016 at 8:17
  • you mean you need see difference between two array or need to see matching items in the array? Commented Sep 30, 2016 at 8:20
  • @Gal Yes, fully booked array is coming from other function which filters specific days from the other array and then sets them in the fully booked array. Matching days array comes from start day and end day which will push items to array from start to the end. Commented Sep 30, 2016 at 8:23
  • @Deepakrao Well have to check if any days from matchingDay array are in the calendar array. So I need to see matching items in the array. (If has matching dates -> return true) Commented Sep 30, 2016 at 8:25
  • 1
    A date is an Object, two date objects will never equal.. Best option is to convert the dates into strings, or numbers etc. Commented Sep 30, 2016 at 8:27

3 Answers 3

1

Your match function should look like this :

findIfMatch = function (haystack, arr){
      var i = 0;//array index
      var j = 0;//array index
      while(j < arr.length && i < haystack.length){

          cur_cal = Date.parse(haystack[i]);
          cur_match = Date.parse(arr[j]);
          if(cur_cal > cur_match){
              j++;
          }else if(cur_cal < cur_match){
              i++;
          }else{
              return true;
          }
      }
      return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Very good, I like this approach since it doesn't build new arrays.
1

To get matching records from two array use this

var calendar =  [
Sun Oct 02 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 09 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 16 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 23 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST)
];
var matchingDays = [
Fri Oct 28 2016 00:00:00 GMT+0300 (EEST),
Sat Oct 29 2016 00:00:00 GMT+0300 (EEST),
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST)
];
var newCalendar = [];
var newMatchingDays = []
$.map(calendar, function(date){
    newCalendar.push(date.toString())
});

$.map(matchingDays, function(date){
    newMatchingDays.push(date.toString())
});

var result = [];
$.map(newCalendar, function (val, i) {
    if ($.inArray(val, newMatchingDays) > -1) {
        result.push(val);
    }
});
console.log(result);

3 Comments

Ah, perfect. So basically all I need to check now is result.length === 0, to get correct true or false for my task?
If you don't mind using third party libs, have a look at underscore intersectionWith, I'll update my answer to show example.
ya now underscore has been changed to lodash it is very good for array of object operation
1

Firstly you can't compare dates like that, Date is an object. eg.

var d1 = new Date('2016-09-30');
var d1 = new Date('2016-09-30');
console.log(d1 === d2); // = false

You would need to loop the array and compare each item, rather than use indexOf. or maybe use the Array.filter(). Or alternatively use and object as a lookup.

eg. If say you had ->

var calendar = {
   "Sun Oct 02 2016 00:00:00 GMT+0300 (EEST)": true,
   "Sun Oct 09 2016 00:00:00 GMT+0300 (EEST)": true,
   "Sun Oct 16 2016 00:00:00 GMT+0300 (EEST)": true,
   "Sun Oct 23 2016 00:00:00 GMT+0300 (EEST)": true,
   "Sun Oct 30 2016 00:00:00 GMT+0300 (EEST)": true
};
if (calendar["Sun Oct 16 2016 00:00:00 GMT+0300 (EEST)"]) {
   console.log("We have date");
}

Notice how I use a string representation for the date, this could be a number too, eg. from Date.getTime().

Here is using Array.filter, I don't think it's as fast a Object lookup's. But here we go.

var calendar = [
   new Date('2016-09-01'),
   new Date('2016-09-12'),
   new Date('2016-09-10')
];

var finddate = new Date('2016-09-12');
var found = calendar.filter(function (a) { return a.getTime() === finddate.getTime();});

And if you don't mind using third party library, try underscore..

eg.

var days = [new Date('2016-09-01'), new Date('2016-09-10'), new Date('2016-09-30')];
var finddays = [new Date('2016-09-01'), new Date('2016-09-30')];

var found = _.intersectionWith(days, finddays, 
   function (a,b) { return a.getTime() === b.getTime(); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.