@idris-maps/yyyy-mm-dd
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

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

npm install @idris-maps/yyyy-mm-dd

Example usage

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:

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.

daysInMonth('2021-01-01') // ['2021-01-01', '2021-01-02', ..., '2021-01-31']

details(day) => DayDetails

Takes a day and returns DayDetails as:

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.

isoWeekday(day) => number

Takes a day and returns the ISO-8601 weekday.

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.

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

interface Range {
  start: string // yyyy-mm-dd
  end: string // yyyy-mm-dd
}

subtract(unit: Unit, n: number, day: string) => string

where Unit is:

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

interface DayValidation {
  valid: boolean
  error?: string
}

weekdayIndex(day) => number

Takes a day and returns the index of the weekday. 0 is Monday.

Readme

Keywords

Package Sidebar

Install

npm i @idris-maps/yyyy-mm-dd

Weekly Downloads

0

Version

0.0.2

License

GPLv2

Unpacked Size

31.5 kB

Total Files

52

Last publish

Collaborators

  • idris-maps