Skip to main content

A DateRange type and related utility functions

Project description

date-ranges

A DateRange type and related utility functions.

Quickstart

Install from pip:

pip install date-ranges

Import and create an instance:

from datetime import date

from date_ranges import DateRange

my_date_range = DateRange(start=date(2023, 5, 10), end=date(2023, 11, 3))

Usage

from datetime import date

from date_ranges import DateRange, MAX_DATE

# end date defaults to MAX_DATE
dr = DateRange(date(2023, 11, 1))

date(2023, 11, 22) in dr
# True

# An empty date (0 days in range):
empty = DateRange.empty()
bool(empty)
# False

# Iteration
for d in DateRange(date(2023, 1, 1), date(2023, 1, 3)):
    print(d)
# 2023-01-01
# 2023-01-02
# 2023-01-03

# Create from strings
dr = DateRange.from_string('20230101-20230103')
repr(dr)
# 'DateRange(start=datetime.date(2023, 1, 1), end=datetime.date(2023, 1, 3))'

# Create range for a single date
dr = DateRange.from_string('20231031')
repr(dr)
# 'DateRange(start=datetime.date(2023, 10, 31), end=datetime.date(2023, 10, 31))'

# Create range for given year-month
dr = DateRange.from_string('202305')
repr(dr)
# 'DateRange(start=datetime.date(2023, 5, 1), end=datetime.date(2023, 5, 31))'

# Create range for multiple full months
dr = DateRange.from_string('202305-202311')
repr(dr)
# 'DateRange(start=datetime.date(2023, 5, 1), end=datetime.date(2023, 11, 30))'

# Formatted print
dr = DateRange(date(2023, 1, 1), date(2023, 1, 3))
dr
# 20230101-20230103
print(f'{dr:H}')   # H for human
# 2023-01-01 to 2023-01-03

# Full months rendered appropriately
dr = DateRange.from_string('202305')
f'{dr:H}'  # H for "human-readable"
# 'May 2023'
f'{dr:C}'  # C for compact
# '202305'
dr
# '202305'

Development

For those developing or maintaining the date-ranges package itself, be sure to install it with the [dev] option to pull in packages used when developing.

pip install --editable .[dev]

When developing, this package uses pre-commit. After the initial clone of the repository, you will need to set up pre-commit with:

# in the top level of the checked-out repository:
pre-commit install

Changelog

0.1.0 released 2025-06-28

  • Add utility functions
    • weekday_after, weekday_before, first_weekday_of_month, last_weekday_of_month

0.0.2 released 2023-11-10

  • Fixed typo

0.0.1 released 2023-11-03

  • Initial Version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

date_ranges-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

date_ranges-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file date_ranges-0.1.0.tar.gz.

File metadata

  • Download URL: date_ranges-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for date_ranges-0.1.0.tar.gz
Algorithm Hash digest
SHA256 af73090e5f8828a18f328238235cdcfd9132715ac32f37b5f5dc2325ae22a050
MD5 161ebf4e0b7229e395e0fa2208bb12f8
BLAKE2b-256 ba05ad421606802a9303115248c9731a47742bd9bea005af44062dc3e27e6ca2

See more details on using hashes here.

File details

Details for the file date_ranges-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: date_ranges-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for date_ranges-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57ba1a8d222bf6f25d624156ac78b3be439b096a821d086db41510a13b4a324d
MD5 a4efe6ac3ba87aae50cd49d8822785c0
BLAKE2b-256 8d4efd67303e212407ba1956d8c137818f8f198ec6b84c79e58b9642f6cfa2fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page