@idris-maps/yyyy-mm-dd
Version:
A date library dealing only with days in the YYYY-MM-DD format
205 lines (137 loc) • 3.83 kB
Markdown
A date library dealing only with days in the `YYYY-MM-DD` format.
* No dependencies.
* All functions with multiple arguments are curried.
* Types are included.
Install
```bash
npm install @idris-maps/yyyy-mm-dd
```
Example usage
```ts
import { add } from '@idris-maps/yyyy-mm-dd'
add('days', 3, '2021-01-01') // 2021-01-04
// or
const addThreeDays = add('days', 3)
addThreeDays('2021-01-01') // 2021-01-04
// or
const addDays = add('days')
const add3Days = addDays(3)
add3Days('2021-01-01') // 2021-01-04
```
where `Unit` is:
```ts
type Unit = 'day'
| 'days'
| 'week'
| 'weeks'
| 'month'
| 'months'
| 'year'
| 'years'
```
and `n` is an integer.
A shorthand for `add('day')` or `add('days')`
A shorthand for `add('month')` or `add('months')`
A shorthand for `add('week')` or `add('weeks')`
A shorthand for `add('year')` or `add('years')`
Takes a day and returns an array of all the days in that month.
```ts
daysInMonth('2021-01-01') // ['2021-01-01', '2021-01-02', ..., '2021-01-31']
```
### `details(day) => DayDetails`
Takes a day and returns `DayDetails` as:
```ts
interface DayDetails {
day: string
index: {
week: number
weekInMonth: number
weekday: number
month: number
},
iso: {
week: number
weekday: number
}
string: {
DD: string
MM: string
YYYY: string
YY: string
}
number: {
day: number
month: number
year: number
}
}
```
Takes a day and returns the first day of that month.
Takes a javascript `Date` and returns a day (YYYY-MM-DD string).
### `isoWeek(day) => number`
Takes a day and returns the [ISO-8601 week](https://en.wikipedia.org/wiki/ISO_week_date).
Takes a day and returns the [ISO-8601 weekday](https://en.wikipedia.org/wiki/Names_of_the_days_of_the_week#Numbered_days_of_the_week).
Takes a day and returns the last day of that month.
Get all month within a range. See `range()` for how to get a range.
```ts
interface Range {
start: string // yyyy-mm-dd
end: string // yyyy-mm-dd
}
```
`month` is a string in the `yyyy-mm` format.
Takes an array of days and returns a `Range`
```ts
interface Range {
start: string // yyyy-mm-dd
end: string // yyyy-mm-dd
}
```
where `Unit` is:
```ts
type Unit = 'day'
| 'days'
| 'week'
| 'weeks'
| 'month'
| 'months'
| 'year'
| 'years'
```
and `n` is an integer.
A shorthand for `subtract('day')` or `subtract('days')`
A shorthand for `subtract('month')` or `subtract('months')`
A shorthand for `subtract('week')` or `subtract('weeks')`
A shorthand for `subtract('year')` or `subtract('years')`
Returns today as a `yyyy-mm-dd` string.
Checks if a value is a day. Returns `DayValidation`
```ts
interface DayValidation {
valid: boolean
error?: string
}
```
Takes a day and returns the index of the weekday. `0` is Monday.