Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method to check if a date is inside a RRULE? #158

Open
gr0g opened this issue Jan 20, 2019 · 5 comments
Open

method to check if a date is inside a RRULE? #158

gr0g opened this issue Jan 20, 2019 · 5 comments
Labels

Comments

@gr0g
Copy link

gr0g commented Jan 20, 2019

Hi!
Is there a method to check if a date/datetime is inside a defined rule? (e.g. returns true/false if datetime is in it)
Thanks!

p.s sorry if I totally missed in - scanned the docs + code but wasn't clear how to do that. Thank you!

@simshaun
Copy link
Owner

No. You would need to loop through the recurrence collection and compare each element to your date.

@gr0g
Copy link
Author

gr0g commented Jan 20, 2019

Thanks Shaun!
Any reason this isn't offered by the library? e.g. isn't this a relatively typical use case?

p.s: do tell if this is something you'd like to add to library / etc as I can contribute

@danielpetrica
Copy link

You could achieve this using transform() by filtering for occurrences happening between day start and end.
Here's my example code

// event recurring daily, It needs at least a start dateTime to start calculating the Occurrences
$rule = new \Recurr\Rule('FREQ=DAILY;UNTIL=20200419T215959Z', '2018-03-21 10:00:00');

// transformer used for the filtering and occurrences generation
$transformer = new \Recurr\Transformer\ArrayTransformer();

// DateTimes used for filtering 
$startDate   = new \DateTime('2019-03-21 00:00:00');
$endDate   = new \DateTime('2019-03-21 23:59:59');

// true so extremes are included
$result = $transformer->transform($rule)->startsBetween($startDate,$endDate, true )->toArray();

// checking if I have at least one occurrence in this day
if (array_count_values($result) > 0) {
   // then It means this day is "included"
}	

@mtachaudhary
Copy link

mtachaudhary commented Apr 1, 2022

I also want its solution. I am trying to check/match a specific date inside the recurrence series using JavaScript.

Someone posted some solution on StackOverflow, but it's not working:

function checkIfToday(rruleStr){
  var RRule = require('rrule').RRule;
  var moment = require('moment');

  var rule = RRule.fromString(rruleStr);

  // Convert all dates into UTC before comparison
  var todayutc          = moment().utc().startOf('day'); // today in UTC
  var nextOccurrence    = rule.after(todayutc, true); // next rule date including today
  var nextOccurutc      = moment(nextOccurrence).utc(); // convert today into utc
  var match             = moment(nextOccurutc).isSame(todayutc, 'day'); // check if 'DAY' is same

  return match;
}

Ref: https://stackoverflow.com/a/37420234/8498688`

@mtachaudhary
Copy link

I also want its solution. I am trying to check/match a specific date inside the recurrence series using JavaScript.

Someone posted some solution on StackOverflow, but it's not working:

function checkIfToday(rruleStr){
  var RRule = require('rrule').RRule;
  var moment = require('moment');

  var rule = RRule.fromString(rruleStr);

  // Convert all dates into UTC before comparison
  var todayutc          = moment().utc().startOf('day'); // today in UTC
  var nextOccurrence    = rule.after(todayutc, true); // next rule date including today
  var nextOccurutc      = moment(nextOccurrence).utc(); // convert today into utc
  var match             = moment(nextOccurutc).isSame(todayutc, 'day'); // check if 'DAY' is same

  return match;
}

Ref: https://stackoverflow.com/a/37420234/8498688`

I have fixed this issue. I was passing a date like this: let date = new Date( Date.UTC(2022, 4, 5) );, but it should be let date = new Date('2022-04-05');. Otherwise, it will not generate a correct date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants