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.
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.
const date = sainsburysDate.fromDate('2015-10-05')
console.log(date.year)
console.log(date.quarter)
console.log(date.period)
console.log(date.week)
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).
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)
console.log(b)
console.log(c)
console.log(d)
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)