Published
Edited
Jul 19, 2019
Fork of rSchedule
Insert cell
Insert cell
rschedule = import('@rschedule/rschedule@0.11.3')
Insert cell
// standardAdapter = import('https://unpkg.com/@rschedule/standard-date-adapter@0.11.0/build/module.js?module')
standardAdapter = import('@rschedule/standard-date-adapter@0.11.0')
Insert cell
// jsonTools = import('https://unpkg.com/@rschedule/json-tools@0.11.0/build/main.js?module')
jsonTools = import('@rschedule/json-tools@0.11.0')
Insert cell
rschedule.RScheduleConfig.defaultDateAdapter = standardAdapter.StandardDateAdapter;
// rschedule.RScheduleConfig.defaultDateAdapter = luxon.LuxonDateAdapter;
Insert cell
// A schedule is analogous to a ruleset in rrule.js
schedule = new rschedule.Schedule({
rrules: [
{
start: new Date(2014, 10, 2),
frequency: "YEARLY",
duration: 1000 * 60 * 60 * 24, // Add a day, duration is specified in milliseconds
count: 10
}
],
dateAdapter: standardAdapter.StandardDateAdapter,
} );
Insert cell
Insert cell
// Check starting schedule
schedule.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
// Create a new rrule
rrule2002 = new rschedule.Rule({
start: new Date(2002,2),
frequency: 'DAILY',
count: 4,
}, {data: 'dsfdfdfd', dateAdapter: standardAdapter.StandardDateAdapter},)
Insert cell
// Add an rrule to the starting schedule
newSchedule1 = schedule.add('rrule', rrule2002)
Insert cell
// Check output
newSchedule1.occurrences().toArray().map(adapter => adapter.date.toLocaleString())
Insert cell
rdate2005 = new Date(2005,6,6)
Insert cell
// Add a rdate to the schedule
newSchedule2 = newSchedule1.add('rdate', rdate2005)
Insert cell
// Check output
newSchedule2.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
// Add a rdate to the schedule
newSchedule3 = newSchedule2.add('exdate', rdate2005)
Insert cell
// Check output
newSchedule3.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
// Add an exrule to the schedule (the same rrule we added)
newSchedule4 = newSchedule3.add('exrule', rrule2002)
Insert cell
// Check output
newSchedule4.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
// Initialize a date
threeDatesWithDuration = new rschedule.Dates({
dates: [new Date(2000), new Date(2001), new Date(2002)],
duration: 32434, //It is possible to set a duration as an array [123, 2134, 233] and this results in applying the array
});
Insert cell
// How do I access the duration of a single date?
threeDatesWithDuration.occurrences //??
Insert cell
Insert cell
newSchedule1.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
newSchedule1
Insert cell
// THIS DOESN'T WORK. Modify a schedule's rrule.
modifiedSchedule = newSchedule1.rrules[0].set(new rschedule.Rule({
start: new Date(2040,1,1),
frequency: 'DAILY',
count: 8,
}))
Insert cell
// THIS DOESN'T WORK. Modify a schedule's rrule.
modifiedSchedule2 = newSchedule1.set(newSchedule1.rrules[0], new rschedule.Rule({
start: new Date(2040,1,1),
frequency: 'DAILY',
count: 8,
}))
Insert cell
modifiedSchedule3 = newSchedule1.set('rrule',
{'0': new rschedule.Rule({
start: new Date(2040,1,1),
frequency: 'DAILY',
count: 16,
})})
Insert cell
// Check output
modifiedSchedule2.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
Insert cell
newSchedule2.occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
newSchedule1.remove('rrule', newSchedule1.rrules[0]).occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
newSchedule2.remove('rdate', newSchedule2.rdates.datetimes[0]).occurrences().toArray().map(adapter => adapter.date.toLocaleString());
Insert cell
md `# Get start and end times, get various slices of schedule`
Insert cell
// Get the first 10 recurring event START times
schedule
.occurrences({ take: 10 })
.toArray()
.map(adapter => adapter.date.toLocaleString());
Insert cell
// Get the first 10 recurring event END times
schedule
.occurrences({ take: 10 })
.toArray()
// .map(adapter => luxon.DateTime.fromJSDate(adapter.date).plus({second: adapter.duration}).toJSDate().toLocaleString())
.map(adapter => adapter.end.toLocaleString())
Insert cell
// Get the event right before a certain time. Analogous to rule.before() in rrule.js
schedule
.occurrences({reverse: true, end: new Date(), take:2})
.toArray()
.map(adapter => adapter.date.toLocaleString());
Insert cell
// Get all the occurences between start date and end date
schedule
.occurrences({start: new Date(2017,1,1), end: new Date()})
.toArray()
.map(adapter => adapter.date.toLocaleString());
Insert cell
// Get the last occurrence in a schedule. Step 1: create generator object
iterator = schedule.occurrences({ reverse: true })
Insert cell
// Get the last occurrence in a schedule. Step 2: yield desired value
end = iterator.next().value.date
Insert cell
// Serialize schedule to json
jsonSchedule = jsonTools.serializeToJSON(schedule);
Insert cell
// Deserialize json to schedule
deserializedSchedule = jsonTools.parseJSON(jsonSchedule, {dateAdapter: standardAdapter.StandardDateAdapter});
Insert cell
// rule = new rschedule.Rule({
// frequency: 'YEARLY',
// byMonthOfYear: [2, 6],
// byDayOfWeek: ['SU', ['MO', 3]],
// start: new Date(2010, 1, 7),
// });
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more