# Beautiful Date
[![PyPI version](https://badge.fury.io/py/beautiful-date.svg)](https://badge.fury.io/py/beautiful-date)
[![Tests](https://github.com/kuzmoyev/beautiful-date/workflows/Tests/badge.svg)](https://github.com/kuzmoyev/beautiful-date/actions)
[![Downloads](https://pepy.tech/badge/beautiful-date)](https://pepy.tech/project/beautiful-date)
Simple and beautiful way to create date and datetime objects in Python.
**Before**:
```python3
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**:
```python3
from beautiful_date import *
d = 25/Mar/2018
t = (25/Mar/2018)[23:45]
```
## Installation
```bash
pip install beautiful-date
```
## Examples
### Create Date
Using months names:
```python3
>>> 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:
```python3
>>> 25/M[3]/2018 # European format
BeautifulDate(2018, 3, 25)
>>> M[3]/25/2018 # US format
BeautifulDate(2018, 3, 25)
```
Or alternatively:
```python3
>>> 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:
```python3
>>> 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:
```python3
>>> (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:
```python3
>>> (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](http://dateutil.readthedocs.io/en/stable/relativedelta.html) from
[dateutil](http://dateutil.readthedocs.io/en/stable/index.html)
Notice that singular time unit (year, month, ...) sets given value, plural (years, months,) adds it.
#### Shortcuts:
```python
>>> 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:
```python3
>>> 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](http://dateutil.readthedocs.io/en/stable/relativedelta.html)).
```python3
>>> 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](http://dateutil.readthedocs.io/en/stable/relativedelta.html)).
#### Weekdays:
Get next Monday:
```python3
>>> d = 29/Mar/2018 # Thursday
>>> d + MO # Equivalent to MO(1)
BeautifulDate(2018, 4, 2)
```
Get second to next Monday:
```python3
>>> d = 29/Mar/2018
>>> d + MO(2)
BeautifulDate(2018, 4, 9)
```
Get last Saturday:
```python3
>>> d = 29/Mar/2018
>>> d - SA
BeautifulDate(2018, 3, 24)
```
Get second to last Saturday:
```python3
>>> d = 29/Mar/2018
>>> d - SA(2)
BeautifulDate(2018, 3, 17)
```
Get second to last Saturday (same as previous):
```python3
>>> d = 29/Mar/2018
>>> d + SA(-2)
BeautifulDate(2018, 3, 17)
```
### Util
#### drange:
You can use `drange` to generate ranges of dates:
```python3
>>> 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:
```python3
>>> 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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/kuzmoyev/beautiful-date",
"name": "beautiful-date",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.5.0",
"maintainer_email": "",
"keywords": "beautiful,date,simple,timedelta,date-range",
"author": "Yevhen Kuzmovych",
"author_email": "kuzmovych.goog@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/d0/cd/da33e942d76ae5294763e62fb4e6504a3bc184618f6e78ec6a0f9c8ae05e/beautiful-date-2.3.0.tar.gz",
"platform": null,
"description": "\n# Beautiful Date\n\n[![PyPI version](https://badge.fury.io/py/beautiful-date.svg)](https://badge.fury.io/py/beautiful-date)\n[![Tests](https://github.com/kuzmoyev/beautiful-date/workflows/Tests/badge.svg)](https://github.com/kuzmoyev/beautiful-date/actions)\n[![Downloads](https://pepy.tech/badge/beautiful-date)](https://pepy.tech/project/beautiful-date)\n\nSimple and beautiful way to create date and datetime objects in Python.\n \n**Before**:\n\n```python3\nfrom datetime import date, datetime\n\nd = date(year=2018, month=3, day=25)\nt = datetime(year=2018, month=3, day=25, hour=23, minute=45)\n```\n \n**After**:\n\n```python3\nfrom beautiful_date import *\n\nd = 25/Mar/2018\nt = (25/Mar/2018)[23:45]\n```\n\n## Installation\n\n```bash\npip install beautiful-date\n```\n\n## Examples\n\n### Create Date\n\nUsing months names:\n\n```python3\n>>> from beautiful_date import *\n\n>>> 25/Mar/2018 # European format\nBeautifulDate(2018, 3, 25)\n>>> Mar/25/2018 # US format\nBeautifulDate(2018, 3, 25)\n```\n \nUsing months numbers:\n \n```python3\n>>> 25/M[3]/2018 # European format\nBeautifulDate(2018, 3, 25)\n>>> M[3]/25/2018 # US format\nBeautifulDate(2018, 3, 25)\n```\n\nOr alternatively:\n\n```python3\n>>> D @ 25/3/2018 # European format (default)\nBeautifulDate(2018, 3, 25)\n\n>>> D = MDY() # Add this at the top of your script to use US format. \n>>> d = D @ 3/25/2018 # US format\nBeautifulDate(2018, 3, 25)\n```\n\nYou can also easily retrieve current date as a `BeautifulDate` object and current time using:\n\n```python3\n>>> D.today()\nBeautifulDate(2020, 8, 24)\n\n>>> D.now()\ndatetime.datetime(2020, 8, 24, 0, 59, 12, 451363)\n\n>>> D.tomorrow()\nBeautifulDate(2020, 8, 25)\n\n>>> D.yesterday()\nBeautifulDate(2020, 8, 23)\n```\n\n### Create Datetime\n\nPrevious methods create `BeautifulDate` objects which are inherited from `date` but can be \neasily extended to `datetime` using indexing/slicing:\n \n```python3\n>>> (Oct/16/1995)[:]\ndatetime.datetime(1995, 10, 16, 0, 0)\n\n>>> (Oct/16/1995)[23]\ndatetime.datetime(1995, 10, 16, 23, 0)\n\n>>> (Oct/16/1995)[23:14]\ndatetime.datetime(1995, 10, 16, 23, 14)\n\n>>> (Oct/16/1995)[23:14:10]\ndatetime.datetime(1995, 10, 16, 23, 14, 10)\n```\n\nYou can also use prefix `D @` if you need months by their numbers: \n \n```python3\n>>> (D @ 16/10/1995)[:]\ndatetime.datetime(1995, 10, 16, 0, 0)\n\n>>> (D @ 16/10/1995)[23]\ndatetime.datetime(1995, 10, 16, 23, 0)\n\n>>> (D @ 16/10/1995)[23:14]\ndatetime.datetime(1995, 10, 16, 23, 14)\n\n>>> (D @ 16/10/1995)[23:14:10]\ndatetime.datetime(1995, 10, 16, 23, 14, 10)\n```\n \n### Date/Datetime manipulations:\n\nThis library also provides simple interface for \n[relativedelta](http://dateutil.readthedocs.io/en/stable/relativedelta.html) from \n[dateutil](http://dateutil.readthedocs.io/en/stable/index.html)\n\nNotice that singular time unit (year, month, ...) sets given value, plural (years, months,) adds it.\n\n#### Shortcuts:\n\n```python\n>>> 5*days.from_today\nBeautifulDate(2023, 9, 17)\n\n>>> 1*hours.from_now\ndatetime.datetime(2023, 9, 12, 12, 53, 56)\n\n>>> 3*days.since(15/Mar/2023)\nBeautifulDate(2023, 3, 18)\n\n>>> 5*days.until_today\nBeautifulDate(2023, 9, 7)\n\n>>> 1*hours.until_now\ndatetime.datetime(2023, 9, 12, 11, 13, 4)\n\n>>> 3*days.until(15/Mar/2023)\nBeautifulDate(2023, 3, 12)\n```\n\n#### Adding/Subtracting/Setting timedeltas:\n\n```python3\n>>> d = 26/Mar/2018\n>>> t = d[12:23:15]\n\n>>> d + 2 * years\nBeautifulDate(2020, 3, 26)\n>>> d - 2 * days\nBeautifulDate(2018, 3, 24)\n\n>>> t + 25 * hours\ndatetime.datetime(2018, 3, 27, 13, 23, 15)\n```\n \nAvailable deltas: `years`, `months`, `weeks`, `days`, `hours`, `minutes`, \n`seconds`, `microseconds`, `leapdays`\n(see [relativedelta](http://dateutil.readthedocs.io/en/stable/relativedelta.html)).\n\n```python3\n>>> d = 26/Mar/2018\n>>> t = d[12:23:15]\n\n>>> d + 2022 * year\nBeautifulDate(2022, 3, 26)\n>>> d += 2 * day\n>>> d\nBeautifulDate(2018, 3, 2)\n\n>>> t + 22 * hour\ndatetime.datetime(2018, 3, 26, 22, 23, 15)\n>>> t += 22 * hour\n>>> t\ndatetime.datetime(2018, 3, 26, 22, 23, 15)\n```\n\nAvailable setters: `year`, `month`, `day`, `hour`, `minute`, `second`, `microsecond`,\n`yearday` and `nlyearday`\n(see [relativedelta](http://dateutil.readthedocs.io/en/stable/relativedelta.html)).\n\n#### Weekdays:\n\nGet next Monday:\n\n```python3\n>>> d = 29/Mar/2018 # Thursday\n>>> d + MO # Equivalent to MO(1)\nBeautifulDate(2018, 4, 2)\n```\n\nGet second to next Monday:\n\n```python3\n>>> d = 29/Mar/2018\n>>> d + MO(2)\nBeautifulDate(2018, 4, 9)\n```\n\nGet last Saturday:\n\n```python3\n>>> d = 29/Mar/2018\n>>> d - SA\nBeautifulDate(2018, 3, 24)\n```\n\nGet second to last Saturday:\n\n```python3\n>>> d = 29/Mar/2018\n>>> d - SA(2)\nBeautifulDate(2018, 3, 17)\n```\n\nGet second to last Saturday (same as previous):\n\n```python3\n>>> d = 29/Mar/2018\n>>> d + SA(-2)\nBeautifulDate(2018, 3, 17)\n```\n \n### Util\n\n#### drange:\n\nYou can use `drange` to generate ranges of dates:\n\n```python3\n>>> for d in drange(27/Mar/1994, 5/Apr/1994):\n... print(d)\n1994-03-27\n1994-03-28\n1994-03-29\n1994-03-30\n1994-03-31\n1994-04-01\n1994-04-02\n1994-04-03\n1994-04-04\n\n>>> for d in drange(27/Mar/1994, 5/Apr/1994, 2*days):\n... print(d)\n1994-03-27\n1994-03-29\n1994-03-31\n1994-04-02\n1994-04-04\n```\n \nand datetimes:\n\n```python3\n>>> for dt in drange((27/Mar/1994)[10:25], (4/Apr/1994)[10:10]):\n... print(dt)\n1994-03-27 10:25:00\n1994-03-28 10:25:00\n1994-03-29 10:25:00\n1994-03-30 10:25:00\n1994-03-31 10:25:00\n1994-04-01 10:25:00\n1994-04-02 10:25:00\n1994-04-03 10:25:00\n\n>>> for dt in drange((27/Mar/1994)[10:25], (4/Apr/1994)[10:10], 20*hours):\n... print(dt)\n1994-03-27 10:25:00\n1994-03-28 06:25:00\n1994-03-29 02:25:00\n1994-03-29 22:25:00\n1994-03-30 18:25:00\n1994-03-31 14:25:00\n1994-04-01 10:25:00\n1994-04-02 06:25:00\n1994-04-03 02:25:00\n1994-04-03 22:25:00\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Simple and beautiful way to create date and datetime objects in Python.",
"version": "2.3.0",
"project_urls": {
"Download": "https://github.com/kuzmoyev/beautiful-date/archive/1.0.tar.gz",
"Homepage": "https://github.com/kuzmoyev/beautiful-date"
},
"split_keywords": [
"beautiful",
"date",
"simple",
"timedelta",
"date-range"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "74485754e015612d79d36d4da499941f8e433a9ce8e96be98fab99dc5511f662",
"md5": "8d421fe04673c7e7273bcd42854ff454",
"sha256": "5d11a87ae24912d78bc0d30db21052b68011a65e1070647eb09d2e11a1f1a610"
},
"downloads": -1,
"filename": "beautiful_date-2.3.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "8d421fe04673c7e7273bcd42854ff454",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.5.0",
"size": 8153,
"upload_time": "2023-09-15T09:23:14",
"upload_time_iso_8601": "2023-09-15T09:23:14.186805Z",
"url": "https://files.pythonhosted.org/packages/74/48/5754e015612d79d36d4da499941f8e433a9ce8e96be98fab99dc5511f662/beautiful_date-2.3.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d0cdda33e942d76ae5294763e62fb4e6504a3bc184618f6e78ec6a0f9c8ae05e",
"md5": "d92cac97de1c4912f2ef33b1f0c0fc53",
"sha256": "bb9db8f572298ce992a1d7542a22e2bb4959c401bfd451ab02dcbf2732054879"
},
"downloads": -1,
"filename": "beautiful-date-2.3.0.tar.gz",
"has_sig": false,
"md5_digest": "d92cac97de1c4912f2ef33b1f0c0fc53",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.5.0",
"size": 7943,
"upload_time": "2023-09-15T09:23:15",
"upload_time_iso_8601": "2023-09-15T09:23:15.720487Z",
"url": "https://files.pythonhosted.org/packages/d0/cd/da33e942d76ae5294763e62fb4e6504a3bc184618f6e78ec6a0f9c8ae05e/beautiful-date-2.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-15 09:23:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kuzmoyev",
"github_project": "beautiful-date",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "beautiful-date"
}