Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smhg/date-period
JavaScript time period iterator
https://github.com/smhg/date-period
Last synced: about 2 months ago
JSON representation
JavaScript time period iterator
- Host: GitHub
- URL: https://github.com/smhg/date-period
- Owner: smhg
- License: mit
- Created: 2013-12-01T11:59:29.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2021-09-22T08:27:48.000Z (over 3 years ago)
- Last Synced: 2024-10-31T18:59:02.905Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 110 KB
- Stars: 11
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
date-period [![ci](https://github.com/smhg/date-period/actions/workflows/ci.yml/badge.svg)](https://github.com/smhg/date-period/actions/workflows/ci.yml)
======
Time period iterator.A time period in this context is an array of Date objects, recurring at regular intervals, over a given period. Based on ISO 8601's [duration](https://en.wikipedia.org/wiki/ISO_8601#Durations) and [repeating interval](https://en.wikipedia.org/wiki/ISO_8601#Repeating_intervals) format.
Mimics PHP's excellent [DatePeriod](http://www.php.net/manual/en/class.dateperiod.php) class.
## Installation
```bash
$ npm install date-period --save
```## Use
### API
#### Start, duration and end date:
`createPeriod({ start: Date, duration: String, end: Date }) ⇒ Array`
#### Start, duration, and number of recurrences:
`createPeriod({ start: Date, duration: String, recurrence: Number }) ⇒ Array`
#### ISO 8601 Repeating time intervals:
`createPeriod({ iso: String }) ⇒ Array`
Note: the `date` and `duration` parameters above can be objects which have, respectively, `toDate` and `toString` methods. This way [moment](http://momentjs.com) objects are supported.
### Example
Require the module
```javascript
const createPeriod = require('date-period');
```#### Define a period
```javascript
let start = new Date('2014-01-01T00:00:00Z'),
duration = 'P1D',
end = new Date('2014-01-05T00:00:00Z'), // not included in result
period;period = createPeriod({ start, duration, end });
// or, with the number of recurrences instead of an end date:
period = createPeriod({ start, duration, recurrence: 3 });// or, with a string formatted as an ISO 8601 repeating interval:
period = createPeriod({ iso: 'R3/2014-01-01T00:00:00Z/P1D' });
```