         time:
           $lte: endTime
       ]
+    startDayQuery = {}
+    startDayQuery["JsonScheduleV1.schedule_days_runs.#{startDayOfWeek}"] = true
+    endDayQuery = {}
+    endDayQuery["JsonScheduleV1.schedule_days_runs.#{endDayOfWeek}"] = true
+    dayQuery =
+      $or: [
+        startDayQuery
+      ,
+        endDayQuery
+      ]
   else
     timeQuery =
       tiploc_code: tiploc
       time:
         $gte: startTime
         $lte: endTime
-
-  day = (date.getDay() + 6) % 7
-  dayQuery = {}
-  dayQuery["JsonScheduleV1.schedule_days_runs.#{day}"] = true
+    dayQuery["JsonScheduleV1.schedule_days_runs.#{startDayOfWeek}"] = true
   Schedules.find
     $and: [
       'JsonScheduleV1.schedule_segment.schedule_location':

        schedule.platform = loc.platform
        break
    schedules.push schedule
  sortedSchedules = _.sortBy schedules, (schedule) ->
    if schedule.arrival?
      return schedule.arrival
    schedule.departure
  return sortedSchedules



  timeOffset or= 60
  getDateScheduleForTiploc(now, tiploc, timeOffset)

getOffsetDayOfWeek = (m) ->
  # Days runs starts with monday at index 0, javascript starts with sunday at
  # index 0. We need to shift it along
  (m.day() + 6) % 7

@getDateScheduleForTiploc = (date, tiploc, timeOffset) ->
  # FIXME: Retrieve schedules for trains that were schedules the previous day
  # but finish after midnight, i.e., day of week is the day before and the last
  # arrival time is after midnight.
  startDate = moment(date)
  endDate = startDate.clone().add('minutes', timeOffset)
  startTime = parseInt(startDate.format('HHmm'))
  endTime = parseInt(endDate.format('HHmm'))

  startDayOfWeek = getOffsetDayOfWeek(startDate)
  endDayOfWeek = getOffsetDayOfWeek(endDate)

  dayQuery = {}
  # Special after midnight case
  if endTime < startTime
    timeQuery =
