All Projects → kuzmoyev → beautiful-date

kuzmoyev / beautiful-date

Licence: MIT license
Simple and beautiful way to create date and datetime objects in Python.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to beautiful-date

Xa
Beautiful & Customizable logger ❤️
Stars: ✭ 78 (+136.36%)
Mutual labels:  simple, beautiful
Delorean
Delorean: Time Travel Made Easy
Stars: ✭ 1,793 (+5333.33%)
Mutual labels:  date, timedelta
Boo
Boo - A beautiful, clean and responsive theme for Ghost.
Stars: ✭ 176 (+433.33%)
Mutual labels:  simple, beautiful
elcalc
➗ Cross-Platform calculator built with Electron!
Stars: ✭ 88 (+166.67%)
Mutual labels:  simple, beautiful
Startup Landing
Collection of free top of the line startup landing templates built using react/nextjs/gatsby. Free to download, simply edit and deploy! Updated weekly!
Stars: ✭ 176 (+433.33%)
Mutual labels:  simple, beautiful
llb
Dead simple event-driven load-balancer
Stars: ✭ 27 (-18.18%)
Mutual labels:  simple
base64.c
Base64 Library in C
Stars: ✭ 60 (+81.82%)
Mutual labels:  simple
dice-simulator
A Python simple Dice Simulator just for fun
Stars: ✭ 17 (-48.48%)
Mutual labels:  simple
Simple-Game-ERC-721-Token-Template
🔮 Very Simple ERC-721 Smart Contract Template to create your own ERC-721 Tokens on the Ethereum Blockchain, with many customizable Options 🔮
Stars: ✭ 83 (+151.52%)
Mutual labels:  simple
Making-elegant-Matlab-figures
A repository comprising multiple functions for making elegant publication-quality figures in MATLAB
Stars: ✭ 44 (+33.33%)
Mutual labels:  beautiful
SimpleList
A simple linked list for Arduino projects
Stars: ✭ 43 (+30.3%)
Mutual labels:  simple
xml.nim
Simple XML parser in pure Nim
Stars: ✭ 16 (-51.52%)
Mutual labels:  simple
common
Metarhia Common Library
Stars: ✭ 55 (+66.67%)
Mutual labels:  date
simplenetes
The sns tool is used to manage the full life cycle of your Simplenetes clusters. It integrates with the Simplenetes Podcompiler project podc to compile pods.
Stars: ✭ 731 (+2115.15%)
Mutual labels:  simple
hijri
Hijri date library for Ruby
Stars: ✭ 56 (+69.7%)
Mutual labels:  date
game-map-editor
game-map-editor
Stars: ✭ 17 (-48.48%)
Mutual labels:  simple
Hacktoberfest-Banned-The-Repo-Guys-Sorry-For-Your-Time-and-effort
A beginner-friendly open source repository to create your first pull request.
Stars: ✭ 27 (-18.18%)
Mutual labels:  simple
Artal
A .PSD parsing library for LÖVE
Stars: ✭ 41 (+24.24%)
Mutual labels:  simple
youtube-lite
No more wasting time on watching random, irrelevant videos on Youtube. https://youtube-lite.js.org
Stars: ✭ 22 (-33.33%)
Mutual labels:  simple
VPAutoComplete
A simple Auto Complete UITextField also support UITableView written in swift 4.2
Stars: ✭ 20 (-39.39%)
Mutual labels:  simple

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)

Available formats (needed only if you create dates using D@):

class DMY(BaseDateFormat):
    _format = 'day', 'month', 'year'

class MDY(BaseDateFormat):
    _format = 'month', 'day', 'year'

class YMD(BaseDateFormat):
    _format = 'year', 'month', 'day'

class YDM(BaseDateFormat):
    _format = 'year', 'day', 'month'

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)

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

Adding/Subtracting/Setting timedeltas:

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

>>> 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].