1.0.0 • Published 10 years ago
recurrence v1.0.0
recurrence
Calculates the diff between dates and returns a nicely formatted string.
Turns an array of dates into:
- every 4 days
- monthly
- every 10 hours
- etc
Installation
npm install --save recurrenceUsage
const recurrence = require('recurrence');
const dates = [
new Date(2016, 1, 1),
new Date(2016, 1, 2)
new Date(2016, 1, 3)
];
console.log(recurrence(dates));Duplicate dates are removed and the order doesn't matter.
Output
{
"count": 1,
"unit": "day/week/month/year",
"recurrence": "Daily"
}In the output recurrence is set to something that you could show directly to a user.
Values look like this:
- every 5 seconds
- every 30 minutes
- hourly
- every 8 hours
- daily
- every 3 weeks
- monthly
- yearly
- every 10 years
Of course you can use the count and unit for your own output, maybe for other languages or otherwise different results.
Options
You can pass an options object as the second argument.
Defaults:
{
"numberAsString": false,
"strict": false,
"noSingleUnits": false
}Override any of these by passing in an object:
recurrence(dates, {
strict: true,
numberAsString: true
});numberAsString
Outputs the number as a text:
- true: 'Every four years'
- false: 'Every 4 years'
strict
Throw an exception if an invalid date is detected. With strict = false just ignores the date.
noSingleUnits
Doesn't format strings as daily or yearly so it returns Every second/hour/month/year etc instead of daily or weekly
Example
const recurrence = require('recurrence');
const leapyears = [
new Date(2016, 1, 29),
new Date(2012, 1, 29)
new Date(2008, 1, 29)
];
const result = recurrence(leapyears, {
numberAsString: true
});
console.log(`Leap year happens ${result.recurrence}!`);
// Leap year happens every four years!Test
npm run testCoverage
npm run cover