Skip to main content

Beautiful Date

PyPI version Tests Downloads

Simple and beautiful way to create date and datetime objects in Python.

Before:

from datetime import date, datetime

d = date(year=2018, month=3, day=25)
t = datetime(year=2018, month=3, day=25, hour=23, minute=45)

After:

from beautiful_date import *

d = 25/Mar/2018
t = (25/Mar/2018)[23:45]

Installation

pip install beautiful-date

Examples

Create Date

Using months names:

>>> from beautiful_date import *

>>> 25/Mar/2018  # European format
BeautifulDate(2018, 3, 25)
>>> Mar/25/2018  # US format
BeautifulDate(2018, 3, 25)

Using months numbers:

>>> 25/M[3]/2018  # European format
BeautifulDate(2018, 3, 25)
>>> M[3]/25/2018  # US format
BeautifulDate(2018, 3, 25)

Or alternatively:

>>> D @ 25/3/2018  # European format (default)
BeautifulDate(2018, 3, 25)

>>> D = MDY()  # Add this at the top of your script to use US format. 
>>> d = D @ 3/25/2018  # US format
BeautifulDate(2018, 3, 25)

You can also easily retrieve current date as a BeautifulDate object and current time using:

>>> D.today()
BeautifulDate(2020, 8, 24)

>>> D.now()
datetime.datetime(2020, 8, 24, 0, 59, 12, 451363)

>>> D.tomorrow()
BeautifulDate(2020, 8, 25)

>>> D.yesterday()
BeautifulDate(2020, 8, 23)

Create Datetime

Previous methods create BeautifulDate objects which are inherited from date but can be easily extended to datetime using indexing/slicing:

>>> (Oct/16/1995)[:]
datetime.datetime(1995, 10, 16, 0, 0)

>>> (Oct/16/1995)[23]
datetime.datetime(1995, 10, 16, 23, 0)

>>> (Oct/16/1995)[23:14]
datetime.datetime(1995, 10, 16, 23, 14)

>>> (Oct/16/1995)[23:14:10]
datetime.datetime(1995, 10, 16, 23, 14, 10)

You can also use prefix D @ if you need months by their numbers:

>>> (D @ 16/10/1995)[:]
datetime.datetime(1995, 10, 16, 0, 0)

>>> (D @ 16/10/1995)[23]
datetime.datetime(1995, 10, 16, 23, 0)

>>> (D @ 16/10/1995)[23:14]
datetime.datetime(1995, 10, 16, 23, 14)

>>> (D @ 16/10/1995)[23:14:10]
datetime.datetime(1995, 10, 16, 23, 14, 10)

Date/Datetime manipulations:

This library also provides simple interface for relativedelta from dateutil

Notice that singular time unit (year, month, ...) sets given value, plural (years, months,) adds it.

Shortcuts:

>>> 5*days.from_today
BeautifulDate(2023, 9, 17)

>>> 1*hours.from_now
datetime.datetime(2023, 9, 12, 12, 53, 56)

>>> 3*days.since(15/Mar/2023)
BeautifulDate(2023, 3, 18)

>>> 5*days.until_today
BeautifulDate(2023, 9, 7)

>>> 1*hours.until_now
datetime.datetime(2023, 9, 12, 11, 13, 4)

>>> 3*days.until(15/Mar/2023)
BeautifulDate(2023, 3, 12)

Adding/Subtracting/Setting timedeltas:

>>> d = 26/Mar/2018
>>> t = d[12:23:15]

>>> d + 2 * years
BeautifulDate(2020, 3, 26)
>>> d - 2 * days
BeautifulDate(2018, 3, 24)

>>> t + 25 * hours
datetime.datetime(2018, 3, 27, 13, 23, 15)

Available deltas: years, months, weeks, days, hours, minutes, seconds, microseconds, leapdays (see relativedelta).

>>> d = 26/Mar/2018
>>> t = d[12:23:15]

>>> d + 2022 * year
BeautifulDate(2022, 3, 26)
>>> d += 2 * day
>>> d
BeautifulDate(2018, 3, 2)

>>> t + 22 * hour
datetime.datetime(2018, 3, 26, 22, 23, 15)
>>> t += 22 * hour
>>> t
datetime.datetime(2018, 3, 26, 22, 23, 15)

Available setters: year, month, day, hour, minute, second, microsecond, yearday and nlyearday (see relativedelta).

Weekdays:

Get next Monday:

>>> d = 29/Mar/2018  # Thursday
>>> d + MO  # Equivalent to MO(1)
BeautifulDate(2018, 4, 2)

Get second to next Monday:

>>> d = 29/Mar/2018
>>> d + MO(2)
BeautifulDate(2018, 4, 9)

Get last Saturday:

>>> d = 29/Mar/2018
>>> d - SA
BeautifulDate(2018, 3, 24)

Get second to last Saturday:

>>> d = 29/Mar/2018
>>> d - SA(2)
BeautifulDate(2018, 3, 17)

Get second to last Saturday (same as previous):

>>> d = 29/Mar/2018
>>> d + SA(-2)
BeautifulDate(2018, 3, 17)

Util

drange:

You can use drange to generate ranges of dates:

>>> for d in drange(27/Mar/1994, 5/Apr/1994):
...     print(d)
1994-03-27
1994-03-28
1994-03-29
1994-03-30
1994-03-31
1994-04-01
1994-04-02
1994-04-03
1994-04-04

>>> for d in drange(27/Mar/1994, 5/Apr/1994, 2*days):
...     print(d)
1994-03-27
1994-03-29
1994-03-31
1994-04-02
1994-04-04

and datetimes:

>>> for dt in drange((27/Mar/1994)[10:25], (4/Apr/1994)[10:10]):
...     print(dt)
1994-03-27 10:25:00
1994-03-28 10:25:00
1994-03-29 10:25:00
1994-03-30 10:25:00
1994-03-31 10:25:00
1994-04-01 10:25:00
1994-04-02 10:25:00
1994-04-03 10:25:00

>>> for dt in drange((27/Mar/1994)[10:25], (4/Apr/1994)[10:10], 20*hours):
...     print(dt)
1994-03-27 10:25:00
1994-03-28 06:25:00
1994-03-29 02:25:00
1994-03-29 22:25:00
1994-03-30 18:25:00
1994-03-31 14:25:00
1994-04-01 10:25:00
1994-04-02 06:25:00
1994-04-03 02:25:00
1994-04-03 22:25:00

Release files for beautiful-date 2.3.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 beautiful-date 2.3.0
File Size Uploaded
beautiful-date-2.3.0.tar.gz 7.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for beautiful-date 2.3.0
File Interpreter ABI Platform
beautiful_date-2.3.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 16.1 kB

Release files / beautiful-date-2.3.0.tar.gz

Download URL beautiful-date-2.3.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bb9db8f572298ce992a1d7542a22e2bb4959c401bfd451ab02dcbf2732054879
BLAKE2b-256 checksum
How to use checksums
d0cdda33e942d76ae5294763e62fb4e6504a3bc184618f6e78ec6a0f9c8ae05e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release files / beautiful_date-2.3.0-py2.py3-none-any.whl

Download URL beautiful_date-2.3.0-py2.py3-none-any.whl
Size 8.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5d11a87ae24912d78bc0d30db21052b68011a65e1070647eb09d2e11a1f1a610
BLAKE2b-256 checksum
How to use checksums
74485754e015612d79d36d4da499941f8e433a9ce8e96be98fab99dc5511f662
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

2.3.0 This release

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.1

1 release file

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