How to Create Recurring Events Using RRule

Learn how to program recurring calendar events using RRule, recurrence rules.

how to create recurring calendar events using rrule

Intro

In this post, we will look at how to program recurring calendar events using the Recurrence Rule, also known as RRule. RRule is a syntax used by calendar providers for communicating recurring events. Consider checking our blog post on how recurring events benefits your users.

We’ll first spend time exploring what recurrence rules are and take a look at examples. Next, we’ll spend time creating recurrence rules and looking at how to program recurring calendar events using RRule. Alternatively, you can follow our live stream on creating recurring calendar events:

Prerequisites

Consider signing up to create a Nylas account for free! Follow Quickstart to link an account to your account credentials. Ensure to save the access token to use below.

Environment

This is optional if you want to follow along and try out the code snippets in JavaScript and Python:

  • If you need to configure your environment to use Node, look at the Environment setup section from our code samples repository. Alternatively, you can check out this post on How to Send Emails with Nylas Node SDK to configure your environment to use Node.
  • If you already have the Nylas Python SDK installed and your environment is configured, then you’re ready to follow along with this post. Otherwise, I recommend that you read the post How to Send Emails with the Nylas Python SDK where the setup and configuration are clearly explained.

What is RRule?

RRule is a calendar standard syntax for creating the same events over some time. As an example, consider Google Calendar UI for creating recurring events:

google calendar custom recurrence

The following sections will focus on the output of working through this UI, a recurrence rule that is a calendar standard syntax.

What are examples of using RRule?

Let’s consider examples of recurring rules. The RRule syntax consists of many unordered keywords; let’s explore some of them to start:

  • FREQ is the only required input and refers to how often the event occurs
  • BYDAY is what day the event occurs
  • BYMONTH is which month the event occurs
  • COUNT is the total occurrences of the event
  • INTERVAL is the time between recurring events

To highlight the above list is non-exhaustive. Here is an overview of different RRule keywords and possible inputs:

recurring event, rrule keywords

You can also get an in-depth overview of recurrence rules by checking out the iCalendar Scheduling Object Specification.

Let’s decipher RRule examples using the above keywords:

  • RRULE:FREQ=WEEKLY;UNTIL=20221230T090000Z;INTERVAL=1;WKST=MO;BYDAY=MO,FR: The event occurs every Monday and Friday until the start of 2023.
  • DTSTART;TZID=America/New_York:20221201T213000 RRULE:FREQ=MONTHLY;COUNT=10;WKST=MO: The event occurs every month starting on December, 2022, on the first day of the month for the next 10 months.
  • RRULE:FREQ=MONTHLY;WKST=MO;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1: This event will be the last working day of every month.

In this section we looked at the different keywords that make up the RRule standard and examples of different RRule.

Creating RRule Syntax

Let’s take a look at how we can generate RRule Syntax. There are many RRule generators online (about +15k search results for rrule generator). I recommend looking at ones available as libraries to consume when creating recurring calendar events.

The library we used in a recent live stream on creating recurring calendar events was rrule.js. What’s awesome about rrule.js is that they have a sandbox environment available online for creating and trying different RRules:

using a rrule library like rrule.js

Let’s look at examples of using rrule.js:

// weeklyRule.js
const { RRule } = require('rrule');

// Create a rule:
const weeklyRule = new RRule({
  freq: RRule.WEEKLY,
  byweekday: RRule.TU,
  dtstart: new Date(2023, 0, 2, 9, 30, 0, 0),
  count: 10,
});

const weekly = weeklyRule.all();
console.log(weeklyRule.toString());

Running weeklyRule.js in the terminal returns:

$ node build/index.js
DTSTART:20230102T143000Z
RRULE:FREQ=WEEKLY;BYDAY=TU;COUNT=10

Let’s look at creating a recurring event that occurs monthly using rrule.js:

// monthyRule.js
const { RRule } = require('rrule');
// Create a monthly rule:
const monthlyRule = new RRule({
  freq: RRule.MONTHLY,
  dtstart: new Date(2023, 0, 4, 17, 30, 0, 0),
  until: new Date(2023, 12, 0),
  count: 10,
});
  
const monthly = monthlyRule.all();
console.log(monthlyRule.toString())

Running monthyRule.js in the terminal returns:

$ node build/index.js
DTSTART:20230104T223000Z
RRULE:FREQ=MONTHLY;UNTIL=20231231T050000Z;COUNT=10

In this section, we looked at creating RRules using rrule.js.

Create Recurring Events

Let’s look at an example of creating recurring calendar events in Python using Nylas from our code samples repository.

import os
from dotenv import load_dotenv
from nylas import APIClient
import datetime

# Loading all the environment variables from `.env` file
load_dotenv()

nylas = APIClient(
   os.environ.get("CLIENT_ID"),
   os.environ.get("CLIENT_SECRET"),
   os.environ.get("ACCESS_TOKEN"),
)

# You need to specify a CALENDAR_ID to create an event for
calendar_id = os.environ.get("CALENDAR_ID")

# Get today’s date
today = datetime.date.today()
# Today’s date at 11:00:00 am
START_TIME = int(datetime.datetime(today.year, today.month, today.day, 15, 00, 0).strftime('%s'))
# Today’s date at 12:00:00 pm
END_TIME = int(datetime.datetime(today.year, today.month, today.day, 16, 0, 0).strftime('%s'))
# Create event draft
event = nylas.events.create()

# Define event elements
event.title = "Coffee date to Discuss Recurring Events!!"
event.location = "NylasHQ!"
event.when = {"start_time": START_TIME, "end_time": END_TIME}
# RRule is a recurring event that occurs once every month
event.recurrence = {'rrule': ['RRULE:FREQ=MONTHLY;COUNT=10;INTERVAL=1;WKST=MO'], 'timezone': 'America/Toronto'}
event.participants = [{"name": "Nylas", "email": 'devrel@nylas.com'}]
event.calendar_id = calendar_id
# We would like to notify participants
event.save(notify_participants=True)

if event.id:
  print("Event created successfully! See you Soon. ")
else:
  print("Oops! There was an error creating the event.")


# Verify if a recurring event is present in the Calendar. 
character_to_search = "Coffee date to Discuss Recurring Events!!"

events = nylas.events.where(calendar_id=calendar_id,title=character_to_search)
# Get the recurrence and participant email from the first invite of the list and perform verification. 
# After that we will assume that for the remaining recurrences, same details are available.
for event in events: 
    if event.recurrence:
      print("Recurring event is generated successfully!")
    if event.participants[0]["email"] == to_email:
      print("Desired invitee's are present.")
    break

Before running the code, ensure to set the .env variables which should include the following availably by creating a free Nylas account following the Quickstart to link an account to your account credentials.

  • CLIENT_ID: The Nylas application ID
  • CLIENT_SECRET: The Nylas application secret
  • ACCESS_TOKEN: The connected user’s access token
  • CALENDAR_ID: The specific calendar to create the event for

Running this code will produce the following results:

python reoccuring_event.py 
Event created successfully! See you Soon. 
Recurring event is generated successfully!
Desired invitee's are present.

Checking your calendar will show the recurring calendar event on the 8th of every month:

example recurring event in google calendar

In this section, we looked at creating a recurring calendar event using RRule syntax and Nylas.

Build Time

Now you can create recurring events using RRule. You can find example code on the Nylas Samples code repository. You can continue building with Nylas and learn more by visiting the Nylas documentation.

You can sign up Nylas for free and start building!

Don’t miss the action, watch our LiveStream Build with Nylas:

You May Also Like

How to create and read Google Webhooks using Kotlin
How to Manage Calendar Events with the Ruby SDK
Introducing Nylas API v3, now generally available

Subscribe for our updates

Please enter your email address and receive the latest updates.