0

I have the following Angular 1.0 controller:

function EventListController($timeout, eventService) {
  var vm = this;
  vm.events = getEvents();
  vm.calendar buildCalendar();
}

The variable vm.events is something like:

vm.events = [
  { date: "2016-09-22T12:05:42.03", id: 12 }, 
  { date: "2016-09-25T12:05:42.03", id: 14 }
];

The variable calendar is the following:

vm.calendar = {
  weeks: [
    { days: [ { date: "2016-09-12T12:05:42.03" }, ... ] }
    { days: [ { date: "2016-09-22T12:05:42.03" }, ... ] }
  ]
}

I need to go through all events dates and add the ID property to the calendar date of the same DAY. Calendar weeks has all days of that month. In this case is September 2016.

How can I do this?

3
  • 1
    You can iterate both objects Commented Sep 21, 2016 at 12:31
  • But how to compare only date and not times? Commented Sep 21, 2016 at 12:49
  • compare like new Date("2016-09-22T12:05:42.03").setHours(0,0,0,0) == new Date("2016-09-22T09:05:42.03").setHours(0,0,0,0) Commented Sep 21, 2016 at 12:54

1 Answer 1

1

You need to iterate both objects. You can use Date constructor to create Date object and the use setHours(0,0,0,0) to set hours, mins.. as zero, then you can only compare date.

var vm = {};
vm.events = [{
  date: "2016-09-22T12:05:42.03",
  id: 12
}, {
  date: "2016-09-25T12:05:42.03",
  id: 14
}];
vm.calendar = {
  weeks: [{
    days: [{
      date: "2016-09-25T09:05:42.03"
    }]
  }]
};

vm.calendar.weeks.forEach(function(w) {
  w.days.forEach(function(d) {
    var e = vm.events.filter(function(e) {
      return new Date(e.date).setHours(0, 0, 0, 0) == new Date(d.date).setHours(0, 0, 0, 0);
    });
    if (e.length) {
      d.id = e[0].id;
    }
    console.log(d)
  })
});

//console.log(vm.calendar.weeks)

Sign up to request clarification or add additional context in comments.

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.