UNPKG

@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
# yyyy-mm-dd 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. ## Usage 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 ``` ## Functions ### `add(unit: Unit, n: number, day: string) => string` where `Unit` is: ```ts type Unit = 'day' | 'days' | 'week' | 'weeks' | 'month' | 'months' | 'year' | 'years' ``` and `n` is an integer. ### `addDays(day, number) => day` A shorthand for `add('day')` or `add('days')` ### `addMonths(day, number) => day` A shorthand for `add('month')` or `add('months')` ### `addWeeks(day, number) => day` A shorthand for `add('week')` or `add('weeks')` ### `addYears(day, number) => day` A shorthand for `add('year')` or `add('years')` ### `daysInMonth(day) => day[]` 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 } } ``` ### `firstDayOfMonth(day) => day` Takes a day and returns the first day of that month. ### `fromJsDate(Date) => day` 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). ### `isoWeekday(day) => number` 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). ### `lastDayOfMonth(day) => day` Takes a day and returns the last day of that month. ### `monthsInRange(Range) => 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. ### `range(day[]) => Range` Takes an array of days and returns a `Range` ```ts interface Range { start: string // yyyy-mm-dd end: string // yyyy-mm-dd } ``` ### `subtract(unit: Unit, n: number, day: string) => string` where `Unit` is: ```ts type Unit = 'day' | 'days' | 'week' | 'weeks' | 'month' | 'months' | 'year' | 'years' ``` and `n` is an integer. ### `subtractDays(day, number) => day` A shorthand for `subtract('day')` or `subtract('days')` ### `subtractMonths(day, number) => day` A shorthand for `subtract('month')` or `subtract('months')` ### `subtractWeeks(day, number) => day` A shorthand for `subtract('week')` or `subtract('weeks')` ### `subtractYears(day, number) => day` A shorthand for `subtract('year')` or `subtract('years')` ### `today() => day` Returns today as a `yyyy-mm-dd` string. ### `validate(any) => DayValidation` Checks if a value is a day. Returns `DayValidation` ```ts interface DayValidation { valid: boolean error?: string } ``` ### `weekdayIndex(day) => number` Takes a day and returns the index of the weekday. `0` is Monday.