sainsburys-date
Version:
Basic functions to convert a Date object into Sainsbury's financial calendar components
47 lines (31 loc) • 1.69 kB
Markdown
# Sainsbury's Date
Basic functions to convert a date object or string into Sainsbury's financial calendar components or vice-versa.
## Usage
Run `npm install sainsburys-date` to install the package into your application, then use `import` (or `require`) to bring it in.
```js
import sainsburysDate from 'sainsburys-date'
```
### `fromDate(date | dateStr)`
This converts a `Date` object or `yyyy-mm-dd` string into it's Sainsbury's calendar parts.
```js
const date = sainsburysDate.fromDate('2015-10-05')
console.log(date.year) // 2015
console.log(date.quarter) // 3
console.log(date.period) // 8
console.log(date.week) // 2
```
### `toDate({ year, quarter, period, week, day })`
This calculates the date represented by the passed Sainsbury's calendar parts. Dates are calculated starting from the year `option`, offset by the `quarter`, `period`, `week` and `day` optional parts (summated in their respective order).
```js
const a = sainsburysDate.toDate({ year: 2015, period: 8, week: 2 })
const b = sainsburysDate.toDate({ year: 2015, week: 30 })
const c = sainsburysDate.toDate({ year: 2017, day: 15 })
const d = sainsburysDate.toDate({ year: 2017, quarter: 4 })
console.log(a) // new Date('2015-10-04')
console.log(b) // new Date('2015-10-04')
console.log(c) // new Date('2017-03-26')
console.log(d) // new Date('2017-12-17')
```
## Sainsbury's Leap Week / Period?
At some point Sainsbury's will need to introduce a "leap" week or period otherwise the start of the financial year will fall into the previous year!
However, a decision on how to introduce this hasn't been made yet and this won't be a problem until `Sun Dec 31 2073` (which will otherwise be the Q1 for 2074)