Published
Edited
Jan 24, 2020
Insert cell
md`# [rSchedule](https://gitlab.com/john.carroll.p/rschedule) example: Get last or next monday in from a specific time zone`
Insert cell
rschedule = import('@rschedule/rschedule@0.11.6')
Insert cell
// dateAdapter = import('@rschedule/standard-date-adapter@1.0.0')
Insert cell
luxonAdapter = import('@rschedule/luxon-date-adapter@0.11.6')
Insert cell
luxon = import('https://unpkg.com/luxon@1.21.3/src/luxon.js?module')
Insert cell
targetDateRrule = luxon.DateTime.fromObject({year: 2020,
month: 1, //starts at 1
day: 24, //starts at 1
hour: 12, //starts at 0
minute: 0, //starts at 0
zone: 'America/Los_Angeles'})
// zone: 'Africa/Asmera'})
// zone: 'America/New_York'})
Insert cell
// dateInThePastWithDesiredWeekday = targetDateRrule.set({weekday: 5}).minus({weeks: 4})
dateInThePastWithDesiredWeekday = targetDateRrule.set({years:1900}).set({weekday: 4})
Insert cell
dateInThePastWithDesiredWeekday.toHTTP()
Insert cell
schedule = new rschedule.Schedule({
rrules: [
{
// start: luxon.DateTime.local(2014,1,2).setZone('America/Los_Angeles'),
start: dateInThePastWithDesiredWeekday,
frequency: "WEEKLY",
}
],
dateAdapter: luxonAdapter.LuxonDateAdapter,
});
Insert cell
schedule2 = schedule.set('timezone', 'America/Los_Angeles')
Insert cell
// Get the weekday right before or after a certain date.
schedule2
.occurrences({reverse: true, end: targetDateRrule, take:1}) // Get last Thurs @ take=1, 2nd to last Thurs take=2
// .occurrences({reverse: false, start: targetDateRrule, take:1}) // Get next Thurs @ take=1, 2nd Thurs take=2
.toArray()
.map(adapter => adapter.date.toString());
Insert cell
md`# Moment way`
Insert cell
moment = require('moment@2.24.0/moment.js')
Insert cell
function nextDay (dayINeed) {
// const dayINeed = 4; // for Thursday
const today = moment().isoWeekday();

// if we haven't yet passed the day of the week that I need:
if (today <= dayINeed) {
// then just give me this week's instance of that day
return moment().isoWeekday(dayINeed);
} else {
// otherwise, give me *next week's* instance of that same day
return moment().add(1, 'weeks').isoWeekday(dayINeed);
}
}
Insert cell
function getDateOfAWeekday (desiredWeekday, referenceDate = luxon.DateTime.local(), next, timeZoneID) {
const weekdays = ({
SUNDAY: 0,
MONDAY: 1,
TUESDAY: 2,
WEDNESDAY: 3,
THURSDAY: 4,
FRIDAY: 5,
SATURDAY: 6,
})
const desiredWeekdayNumber = weekdays[desiredWeekday]
const referenceDateWeekday = referenceDate.weekday
if (next) {
// if we haven't yet passed the day of the week that I need:
if (referenceDateWeekday <= desiredWeekdayNumber) {
// then just give me this week's instance of that day
// return moment().isoWeekday(desiredWeekdayNumber);
return referenceDate.set({weekday: desiredWeekdayNumber}).setZone(timeZoneID).toString()
} else {
// otherwise, give me *next week's* instance of that same day
return referenceDate.plus({weeks: 1}).set({weekday: desiredWeekdayNumber}).setZone(timeZoneID).toString()
// return moment().add(1, 'weeks').isoWeekday(desiredWeekdayNumber);
}
} else {
}
}
Insert cell
targetDate = luxon.DateTime.fromObject({year: 2020,
month: 1, //starts at 1
day: 24, //starts at 1
hour: 12, //starts at 0
minute: 0, //starts at 0
// zone: 'America/Los_Angeles'}).toString()
// zone: 'Africa/Asmera'}).toString()
zone: 'America/New_York'}).toString()
Insert cell
luxon.DateTime.fromISO('2020-01-24T12:00:00.000Z').setZone('America/New_York', { keepLocalTime: true }).toString()
Insert cell
targetDate.toString()
Insert cell
getDateOfAWeekday ("FRIDAY", targetDate, true, 'America/New_York')
Insert cell
luxon.DateTime.fromISO('2020-01-24T01:56:46.456Z').toString()
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more