date-queries

1.0.2 • Public • Published

Date Queries

Build Status npm version

JS function to check date or date range against common queries or custom functions.

Getting Started

Install:

yarn add date-queries

How to use

Pass dtstart and dtend (dtend can be set to undefined) and queries to check the dates against.

The queries can be either one of the predefined (see below) or a custom function that accepts dtstart and dtend and returns a value (typically string) if truthy.

The dateQueries function returns name of the first matching query (for example tomorrow) or value of the custom function. Queries are evaluated in order as specified.

queries = [
  'today',
  'tomorrow',
  'this-week'
]

# dateQueries(dtstart, dtend, queries)
dateQueries(new Date(), null, queries) # => 'today'

With custom function:

import { isWednesday, isWithinRange, setDay } from 'date-fns'

queries = [
  ((dtstart, dtend) ->
    if dtend
      # does the range cover coming Wednesday?
      isWithinRange(setDay(new Date(), 3), dtstart, dtend)
    else
      isWednesday(dtstart)
  )
]

dateQueries(dtstart, dtend, queries)

Available queries

  • now, today
  • tomorrow
  • yesterday
  • this-week
  • next-week
  • past-week
  • this-month
  • next-month
  • past-month
  • this-year
  • next-year
  • past-year
  • next
  • past
  • nearest-weekend
  • weekend
  • rest-of-this-week

Development

Running the tests:

yarn test
yarn test --watch

Building

yarn build

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tomasc/date-queries.

License

The library is available as open source under the terms of the MIT License.

/date-queries/

    Package Sidebar

    Install

    npm i date-queries

    Weekly Downloads

    1

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    273 kB

    Total Files

    11

    Last publish

    Collaborators

    • tomasc