Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:09:11+00:00 2026-06-17T22:09:11+00:00

I’m working on a Javascript/jQuery calendar which includes a month view and a day

  • 0

I’m working on a Javascript/jQuery calendar which includes a month view and a day view. Clicking the days will change the date, which will update the date variables in the day view.

The day view is split up into half hour segments from midnight to 11:00 PM. Clicking on any half hour <tr> (the day view is a table) will create an event between that time clicked and an hour in the future, as well as append a div on top of the calendar, spanning the range of time and positioned at the correct starting point (each pixel is a minute…)

There is a problem, however. If you create an “event” between a certain time span where there is already one in place, they overlap. This is the default behavior, obviously, but what I would like to happen is that if an event is created between a range of dates that is already occupied by an event, they align side by side so that they’re not overlapping.

This resembles the behavior seen in the iCal app for mac:
enter image description here

Now my first thought to achieve such a goal was to use collision detection, but all the jQuery plugins for this are bloated or require the elements to be draggable.

Then I thought there might be a way in CSS to do this, where if two elements are overlapping, they split the width evenly.

Then I thought that’s ridiculously far fetched, so I’m wondering how I can achieve this as easily as possible.

I’ll post the full code in a jsFiddle, but for the most important function would be insertEvent which looks like this:

    function insertEvent(start, end){

        var end_minutes = new Date(end).getMinutes();
        var end_border = new Date(new Date(end).setMinutes(end_minutes + 2));

        //$(".day_date").html(start + "<br />" + end);
        var diff = Math.abs(end_border - new Date(start));
        var minutes = Math.floor((diff/1000)/60);

        var start_element = $("td").find("[data-date='" + start + "']");
        var offset = start_element.offset().top - $(".second").offset().top;

        var this_element = $("<div class='event' style='height:" + minutes + "px;margin-top:" + offset + "px;'></div>");

        $(".right").prepend(this_element);

    }

This takes two parameters in the javascript new Date() format, one for the start date and one for the end date.

The fiddle: http://jsfiddle.net/charlescarver/HwdwL/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:09:12+00:00Added an answer on June 17, 2026 at 10:09 pm

    One of the the problems I see with your approach is that there isn’t a structure to the storage of the data. I’ve built a calendar in Javascript before and it’s not easy work. First, make sure you have some kind of abstraction for the calendar event. Something like:

    function CalendarEvent(startDateTime, endDateTime) {
      this.startDateTime = startDateTime;
      this.endDateTime = endDateTime;
    }
    
    CalendarEvent.prototype.start = function() {
      return this.startDateTime.getTime();
    };
    
    CalendarEvent.prototype.end = function() {
      return this.endDateTime.getTime();
    };
    
    CalendarEvent.new = function(startDateTime, endDateTime) {
      // This is a little factory method. It prevents calendar events 
      // from having end times that fall before the start time.
      // USE THIS TO INSTANTIATE A NEW CALENDAR EVENT
      if(endDateTime.getTime() < startDateTime.getTime()) {
        throw new Error("End time falls before start time");
      }
      return new CalendarEvent(startDateTime, endDateTime);
    };
    
    CalendarEvent.compare = function(eventOne, eventTwo) {
      // this is a class method to compare two events
      // If used with sort it will sort by startDateTime
      return eventOne.start() - eventTwo.start();
    };
    
    // ... add any other methods you need
    

    Next you’re going to want to sort the calendar events. I would sort by start time. Then once it is sorted you can actually re-render everything when changes are made. As long as you sort correctly, determining if a calendar event collides is as simple as this:

    CalendarEvent.prototype.intersects = function(otherEvent) {
      // If the other event starts after this one ends 
      // then they don't intersect
      if(otherEvent.start() > this.end()) {
        return false;
      }
      // If the other event ends before this one starts
      // then they don't intersect
      if(otherEvent.end() < this.start()) {
        return false;
      }
      // Everything else is true
      return true;
    };
    

    Because the data is sorted you know that if two or more calendar events intersect they will have to share the space. Granted, you must think about a few things when you divide the space. Do you want a naive implementation where you just share the space equally from left to right (left having the earliest start time). If so your visual representation could look like this if it had 4 events that shared a space (each block is an event):

    naive rendering

    However if your events have strange shapes they might cause your calendar to look strange. Consider the following:

    possible scenario

    In this instance event 2 takes up a lot of vertical space and all the space underneath event 1 is unused. Maybe for a better UX you don’t want that kind of thing to happen. If so you should design your rendering algorithm accordingly. Just remember that it is probably easiest to re-render on every change that you encounter, but it’s all about how you store the data. If you do not store the data in some kind of structure that is easily traversed then you won’t be able to do this kind of thing.

    But to complete the answer to your question, here is a fairly naive example. I haven’t tested it so this is a pretty big assumption of it working. It is not entirely complete you will have to edit the rendering for yourself. This is merely to give you an idea of how to get it to work. It could definitely look prettier:

    function renderCalendarEvents(calendarEvents) {
      // Sort the calendar events (assuming calendarEvents is an array)
      var sortedEvents = calendarEvents.sort(CalendarEvent.compare);
      var index = 0;
      // renderEvents is an anonymous function that will be called every time 
      // you need to render an event
      // it returns it's columnDivisor.
      var renderEvent = function(position) {
        var currentEvent = sortedEvents[index];
        var nextEvent = sortedEvents[index + 1];
        // The default column divisor is determined by
        // the current x-position + 1
        var columnDivisor = position + 1;
        // Increment before any recursion
        index += 1;
        // Check if nextEvent even exists
        if(nextEvent) {
          // If the nextEvent intersects with the current event
          // then recurse
          if(currentEvent.intersects(nextEvent)) {
            // We need to tell the next event that it starts at the
            // column position that is immediately +1 to the current event
            columnDivisor = renderEvent(position + 1);
          }
        }
        // placeEvent() is some function you can call to actually place 
        // the calendar event element on the page
        // The position is the x-position of the current event
        // The columnDivisor is a count of the amount of events sharing this column
        placeEvent(currentEvent, position, columnDivisor);
        return columnDivisor;
      };
      while(true) {
        // render events until we're done
        renderEvent(0);
        if(index >= sortedEvents.length) {
          break;
        }
      }
    }
    

    Essentially the idea with this particular algorithm is that if the nextEvent on the list exists and that event intersects with the currentEvent then we need to split the width of the currentEvent. It keeps on recursing until it finds no more intersections then it makes it’s way back up the chain of recursive calls. I skipped the actual DOM manipulation logic because really the hard part is determining how much you need to split the actual column in order to get these events to fit. So hopefully this all makes a little bit of sense.

    EDIT:

    To be much more clear, in order to add this to your existing code I would replace your insertEvent function with something like this. I don’t write all of the logic for you so you’ll have to do some of your own writing. But that’s half the fun :-).

    function insertEvent(start, end) {
      var newEvent = Calendar.new(start, end);
      // you'll have to store the array somewhere.
      // i'm just assuming some kind of global right now
      eventsArray.push(newEvent);
      // You'll want to destroy any event elements
      destroyCurrentEventElements();
      // Now run the rendering function
      renderCalendarEvents(eventsArray);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.