Skip to main content

dt-range

Generators for datetime.

Usage

You can use pip to install this package.

$ pip install dt-range

If you use it, import dt_range.

Note the slight difference from this package name.

from dt_range import weekrange ,daterange ,hourrange ,minuterange ,secondrange ,millisecondrange ,microsecondrange ,timedeltarange

Examples

Here is an example of usage using START and END.

weekrange

weekrange can be used to get the date for each week.

from datetime import datetime

from dt_range import weekrange

START = datetime(2021, 1, 1)
END = datetime(2021, 1, 31)

for dt in weekrange(START, END):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-08 00:00:00
# 2021-01-15 00:00:00
# 2021-01-22 00:00:00
# 2021-01-29 00:00:00

for dt in weekrange(START, END, weeks=2):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-15 00:00:00
# 2021-01-29 00:00:00

daterange

daterange can be used to get the date for each day.

from datetime import datetime

from dt_range import daterange

START = datetime(2021, 1, 1)
END = datetime(2021, 1, 31)

for dt in daterange(START, END):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-02 00:00:00
# 2021-01-03 00:00:00
# ...
# 2021-01-29 00:00:00
# 2021-01-30 00:00:00
# 2021-01-31 00:00:00


for dt in daterange(START, END, days=4):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-05 00:00:00
# 2021-01-09 00:00:00
# 2021-01-13 00:00:00
# 2021-01-17 00:00:00
# 2021-01-21 00:00:00
# 2021-01-25 00:00:00
# 2021-01-29 00:00:00

hourrange

hourrange can get the datetime for each hour.

datetime will be 0:00 if you don't specify the hour argument, so you may need to specify 23:00 in some cases.

from datetime import datetime

from dt_range import hourrange

START = datetime(2021, 1, 1)
END = datetime(2021, 1, 1, 23)


for dt in hourrange(START, END):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-01 01:00:00
# 2021-01-01 02:00:00
# 2021-01-01 03:00:00
# ...
# 2021-01-01 21:00:00
# 2021-01-01 22:00:00
# 2021-01-01 23:00:00

for dt in hourrange(START, END, hours=4):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-01 04:00:00
# 2021-01-01 08:00:00
# 2021-01-01 12:00:00
# 2021-01-01 16:00:00
# 2021-01-01 20:00:00

Other

You can also use minuterange, secondrange, etc. in the same way.

If you want to break it into smaller time segments, use timedeltarange.

from datetime import datetime, timedelta

from dt_range import timedeltarange

START = datetime(2021, 1, 1)
END = datetime(2021, 1, 31)


for dt in timedeltarange(
    START,
    END,
    timedelta(days=5, hours=4, minutes=3, seconds=2, microseconds=1),
):
    print(dt)
# 2021-01-01 00:00:00
# 2021-01-06 04:03:02.000001
# 2021-01-11 08:06:04.000002
# 2021-01-16 12:09:06.000003
# 2021-01-21 16:12:08.000004
# 2021-01-26 20:15:10.000005

GitHub Actions

Metadata

Release files for dt-range 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dt-range 1.0
File Size Uploaded Provenance
dt-range-1.0.tar.gz 7.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dt-range 1.0
File Interpreter ABI Platform
dt_range-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / dt-range-1.0.tar.gz

Download URL dt-range-1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
98ec5a7e5f71591ec71f81f6be2f75a64ca3a37cf44ab0d78aca13013eb0d770
BLAKE2b-256 checksum
How to use checksums
43c7a1d2bc4a9023a448328346212fa15189669a87f8a163e41557e1b39e3d3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / dt_range-1.0-py3-none-any.whl

Download URL dt_range-1.0-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
921c66b93bb3b27c7eb460d6808659df63efe59c4ebb461661d60aa8a94f3227
BLAKE2b-256 checksum
How to use checksums
51cea9e8a1f6494e1ee09af2274bd243fb303a3fff3a77ebc53b2f959852c17e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Security for dt-range 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page