---
title: "Javascript Timeline Day/Week View Switcher Example | Mobiscroll"
component: "Timeline"
framework: "JavaScript"
library_version: "6.1.5"
url: "https://demo.mobiscroll.com/javascript/timeline/switching-day-week-work-week-timeline"
---

# Day/Week View Switcher - JavaScript Timeline demo

Build resource schedules with day, work-week, and full-week views on a Javascript Timeline — switch views from the header to control time-grid detail. Plain JavaScript API.

- [Live demo](https://demo.mobiscroll.com/javascript/timeline/switching-day-week-work-week-timeline)
- [View code on GitHub](https://github.com/acidb/mobiscroll-demos-javascript/tree/main/src/demos/eventcalendar/timeline/switching-day-week-work-week-timeline)
- This demo in other frameworks: [jQuery](https://demo.mobiscroll.com/jquery/timeline/switching-day-week-work-week-timeline.md), [Angular](https://demo.mobiscroll.com/angular/timeline/switching-day-week-work-week-timeline.md), [React](https://demo.mobiscroll.com/react/timeline/switching-day-week-work-week-timeline.md), [Vue](https://demo.mobiscroll.com/vue/timeline/switching-day-week-work-week-timeline.md)

## Description

Scheduling people, teams or an entire workforce requires different views. Sometimes you'll be needing a helicopter view of a week and other times you might want to dig into a specific day. This all can be served through a simple switching mechanism implemented with a segmented control right in the header of the calendar.

By [dynamically changing the options](https://mobiscroll.com/docs/javascript/eventcalendar/api) you can set the level of detail you want to see. *Eg. Show a daily timeline with hourly steps, show the work week with the same resolution or zoom out for a full week-view with 12 hour steps.*

**Want to see how to set other views?** [Check out this example for switching views →](https://demo.mobiscroll.com/javascript/scheduler/switching-calendar-scheduler-agenda)

## What this demo shows

- A desktop scheduler timeline with days arranged horizontally and resources listed as rows on the left.
- **Header navigation** The month and year label opens date navigation, the previous and next buttons move between weeks, and the Today button returns to the current date.
- **View switcher** The header includes Day, Work week, and Week options, with Week selected by default.
- **Day view** Selecting Day updates the timeline to a single-day timeline view.
- **Work week view** Selecting Work week updates the timeline to show Monday through Friday.
- **Week view** Selecting Week updates the timeline to show a full Sunday-to-Saturday week.
- **Week strip** The strip below the header shows the selected week from Sunday to Saturday, with the current date highlighted.
- **Time grid** The timeline displays hourly columns across the visible day range.
- **Invalid time ranges** Early-morning and late-evening hours are blocked on weekdays, appear with a gray background, and do not allow event creation or event movement.
- **Invalid weekend** Saturdays and Sundays are blocked, so the weekend columns appear with a gray background and do not allow event creation.
- **Resources** Each resource is displayed as a separate timeline row with an avatar, person name, and job title.
- **Current time** A vertical blue line with a time label marks the current time.
- **Hover behavior** Hovering over the time grid shows a time indicator that follows the cursor in 15-minute increments.
- **Event cards** Events are shown as colored cards with a left color stripe, bold title, and time range.
- **Date positioning** Events are positioned by their assigned resource, date, start time, and end time.
- **Event interaction** Events highlight on hover and expose drag and resize handles for moving the event or changing its duration.
- **Event selection** Clicking an event selects and highlights it.
- **Event creation** New events can be created by double-clicking the timeline or by clicking and dragging across a time range.
- **Scrolling** The timeline supports horizontal and vertical scrolling for navigating through time and resources.

## Best for

- **Workforce scheduling** Switching between day, work-week, and full-week views for teams that need both detailed daily planning and broader weekly coverage.
- **Resource planning** Assigning events to people, teams, rooms, equipment, or other resources that need separate rows in a timeline.
- **Shift and availability planning** Blocking non-working hours and weekends so users schedule only inside allowed time ranges.
- **Operational calendars** Reviewing many scheduled items across multiple resources while keeping resource details visible on the left.
- **Staffing dashboards** Showing names, roles, and avatars next to each resource so planners can identify the right person quickly.
- **Drag-and-drop scheduling flows** Creating, moving, and resizing events directly on the timeline while preventing updates in invalid time ranges.

## API used in this demo

- [`theme`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-theme) - option
- [`themeVariant`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-themeVariant) - option
- [`clickToCreate`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-clickToCreate) - option
- [`dragToCreate`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-dragToCreate) - option
- [`dragToMove`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-dragToMove) - option
- [`dragToResize`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-dragToResize) - option
- [`view`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-view) - option
- [`resources`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-resources) - option
- [`renderResource`](https://mobiscroll.com/docs/javascript/eventcalendar/api#renderer-renderResource) - renderer
- [`renderHeader`](https://mobiscroll.com/docs/javascript/eventcalendar/api#renderer-renderHeader) - renderer
- [`invalid`](https://mobiscroll.com/docs/javascript/eventcalendar/api#opt-invalid) - option
- [`onEventCreateFailed`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventCreateFailed) - event
- [`onEventUpdateFailed`](https://mobiscroll.com/docs/javascript/eventcalendar/api#event-onEventUpdateFailed) - event
- [`setOptions`](https://mobiscroll.com/docs/javascript/eventcalendar/api#method-setOptions) - method
- [`setEvents`](https://mobiscroll.com/docs/javascript/eventcalendar/api#method-setEvents) - method

## Code

### JS

```js
mobiscroll.setOptions({
  theme: 'ios',
  themeVariant: 'light'
});

var calendar = mobiscroll.eventcalendar('#demo-day-work-week-view', {
  clickToCreate: true,
  dragToCreate: true,
  dragToMove: true,
  dragToResize: true,
  view: {
    timeline: {
      type: 'week',
    },
  },
  resources: [
    {
      id: 1,
      name: 'Ryan',
      color: '#ff0101',
      title: 'Cloud System Engineer',
      img: 'https://img.mobiscroll.com/demos/m1.png',
    },
    {
      id: 2,
      name: 'Kate',
      color: '#239a21',
      title: 'Help Desk Specialist',
      img: 'https://img.mobiscroll.com/demos/f1.png',
    },
    {
      id: 3,
      name: 'John',
      color: '#8f1ed6',
      title: 'Application Developer',
      img: 'https://img.mobiscroll.com/demos/m2.png',
    },
    {
      id: 4,
      name: 'Mark',
      color: '#01adff',
      title: 'Network Administrator',
      img: 'https://img.mobiscroll.com/demos/m3.png',
    },
    {
      id: 5,
      name: 'Sharon',
      color: '#d8ca1a',
      title: 'Data Quality Manager',
      img: 'https://img.mobiscroll.com/demos/f2.png',
    },
  ],
  renderResource: function (resource) {
    return (
      '<div class="md-work-week-cont">' +
      '<div class="md-work-week-name">' +
      resource.name +
      '</div>' +
      '<div class="md-work-week-title">' +
      resource.title +
      '</div>' +
      '<img class="md-work-week-avatar" src="' +
      resource.img +
      '"/>' +
      '</div>'
    );
  },
  renderHeader: function () {
    return (
      '<div mbsc-calendar-nav class="md-work-week-nav"></div>' +
      '<div class="md-work-week-picker">' +
      '<label>Day<input mbsc-segmented type="radio" name="view" value="day" class="md-view-change"></label>' +
      '<label>Work week<input mbsc-segmented type="radio" name="view" value="workweek" class="md-view-change"></label>' +
      '<label>Week<input mbsc-segmented type="radio" name="view" value="week" class="md-view-change" checked></label>' +
      '</div>' +
      '<div mbsc-calendar-prev class="md-work-week-prev"></div>' +
      '<div mbsc-calendar-today class="md-work-week-today"></div>' +
      '<div mbsc-calendar-next class="md-work-week-next"></div>'
    );
  },
  invalid: [
    {
      start: '00:00',
      end: '06:00',
      recurring: {
        repeat: 'weekly',
        weekDays: 'MO,TU,WE,TH,FR',
      },
    },
    {
      start: '20:00',
      end: '23:59',
      recurring: {
        repeat: 'weekly',
        weekDays: 'MO,TU,WE,TH,FR',
      },
    },
    {
      recurring: {
        repeat: 'weekly',
        weekDays: 'SA,SU',
      },
    },
  ],
  onEventCreateFailed: function () {
    mobiscroll.toast({
      message: "Can't schedule outside of working hours",
    });
  },
  onEventUpdateFailed: function () {
    mobiscroll.toast({
      message: "Can't schedule outside of working hours",
    });
  },
});

document.querySelectorAll('.md-view-change').forEach(function (elm) {
  elm.addEventListener('change', function (ev) {
    switch (ev.target.value) {
      case 'day':
        calendar.setOptions({
          view: {
            timeline: { type: 'day' },
          },
        });
        break;
      case 'workweek':
        calendar.setOptions({
          view: {
            timeline: {
              type: 'week',
              startDay: 1,
              endDay: 5,
            },
          },
        });
        break;
      case 'week':
        calendar.setOptions({
          view: {
            timeline: {
              type: 'week',
            },
          },
        });
        break;
    }
  });
});

mobiscroll.getJson(
  'https://trial.mobiscroll.com/timeline-events/',
  function (events) {
    calendar.setEvents(events);
  },
  'jsonp',
);
```

### HTML

```html
<div id="demo-day-work-week-view" class="md-switching-timeline-view-cont"></div>
```

### CSS

```css
.md-work-week-cont {
    position: relative;
    padding-left: 50px;
}

.md-work-week-avatar {
    position: absolute;
    max-height: 50px;
    max-width: 50px;
    top: 21px;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    left: 20px;
}

.md-work-week-name {
    font-size: 16px;
}

.md-work-week-title {
    font-size: 12px;
    margin-top: 5px;
}

.md-switching-timeline-view-cont .mbsc-segmented {
    max-width: 350px;
    margin: 0 auto;
    padding: 1px;
}

.md-work-week-picker {
    flex: 1 0 auto;
}

.md-work-week-nav {
    width: 200px;
}

.md-switching-timeline-view-cont .mbsc-timeline-resource {
    display: flex;
    align-items: center;
}

.md-switching-timeline-view-cont .mbsc-timeline-resource-col {
    width: 205px;
}

/* material header order */

.mbsc-material.md-work-week-prev {
    order: 1;
}

.mbsc-material.md-work-week-next {
    order: 2;
}

.mbsc-material.md-work-week-nav {
    order: 3;
}

.mbsc-material .md-work-week-picker {
    order: 4;
}

.mbsc-material .md-work-week-today {
    order: 5;
}

/* windows header order */

.mbsc-windows.md-work-week-nav {
    order: 1;
}

.mbsc-windows.md-work-week-prev {
    order: 2;
}

.mbsc-windows.md-work-week-next {
    order: 3;
}

.mbsc-windows .md-work-week-picker {
    order: 4;
}

.mbsc-windows .md-work-week-today {
    order: 5;
}
```

## Implementation notes

- Set `timeline: { type: 'week' }` as the default view. The three switchable views are:
  - **Day**: `timeline: { type: 'day' }`
  - **Work week**: `timeline: { type: 'week', startDay: 1, endDay: 5 }`
  - **Week** (default): `timeline: { type: 'week' }`
- Use `renderHeader` (Angular: `headerTemplate`, Vue: `#header` slot) with the same structure as the event-listing demo: `CalendarNav`, a centered `SegmentedGroup` with three `Segmented` options (`day` / `workweek` / `week`), then `CalendarPrev`, `CalendarToday`, `CalendarNext`. When the selection changes, replace the active view config accordingly.
- Define 5 people resources, each with `id`, `name`, `color`, a `title` (job title string), and an `img` (avatar URL from `https://img.mobiscroll.com/demos/`).
- Use `renderResource` (Angular: `resourceTemplate`, Vue: `#resource` slot) to render a custom resource label showing the person's avatar, name, and job title.
- Define an `invalid` array to block off non-working time on all resources:
  - `{ start: '00:00', end: '06:00', recurring: { repeat: 'weekly', weekDays: 'MO,TU,WE,TH,FR' } }` - early morning
  - `{ start: '20:00', end: '23:59', recurring: { repeat: 'weekly', weekDays: 'MO,TU,WE,TH,FR' } }` - late evening
  - `{ recurring: { repeat: 'weekly', weekDays: 'SA,SU' } }` - full weekend
- Handle `onEventCreateFailed` and `onEventUpdateFailed` by showing a `Toast` with the message "Can't schedule outside of working hours". This fires when drag-to-create or drag-to-move lands in an invalid slot.
- Load events remotely via `getJson` from the trial endpoint.

## Run it in your project

1. If not signed up yet, or don't own a license, you can [start a free trial](https://mobiscroll.com/starttrial).

2. Clone the repository:

   ```
   git clone https://github.com/acidb/mobiscroll-demos-javascript.git
   ```

3. If not installed yet, install the Mobiscroll CLI from NPM:

   ```
   npm install -g @mobiscroll/cli
   ```

4. Config Mobiscroll - open a terminal window in the root directory and run:

   ```
   mobiscroll config javascript
   ```

5. Install dependencies - open a terminal window in the root directory and run:

   ```
   npm install
   ```

6. Run the project:

   ```
   npm run dev
   ```

On a trial, add `--trial` to the config command: `mobiscroll config javascript --trial`.

Running a demo locally requires a Mobiscroll trial or a license; setup is per framework at https://mobiscroll.com/docs/javascript/getting-started/installation

## Related demos

- Previous: [Daily Event Listing](https://demo.mobiscroll.com/javascript/timeline/event-listing.md) - Show events as full-day labels on a Javascript Timeline — useful for spotting overloaded resources and managing schedules without an hourly time grid.
- Next: [Data & Display Timezone](https://demo.mobiscroll.com/javascript/timeline/setting-the-timezone.md) - Set separate data and display timezones on a Javascript Timeline — configure per-resource event timezone display using Luxon, Moment-timezone, or Day.js.
