Skip to main content

One friendly python function for parsing and converting to and between datetime.datetime, int, and string objects

Project description

date_to - The easy date converter

PyPI - Python Version GitHub PyPI

Tests GitHub Pipenv locked dependency version

Convert any date to another with just one friendly function

date_to() will convert your dates between datetime objects, unix timestamps, and strings. No more boilerplate and headaches of trying to keep track of your dates and their (lack) of timezones.

date_to() utilises the dateparser library for string parsing, enabling many kinds of string representations of time to be converted into machine interpretable dates. All output dates are rounded to second precision.

Default timezone conversion is to UTC. If you wish to convert your date to another timezone, simply provide the abbreviation of your desired timezone for the optional timezone= keyword argument.

Installation

The date_to library is available on PyPi and easily installed using pip:

pip install date_to

Basic Use

from date_to import date_to

some_date = "2001-09-11 17:20 EDT"

a = date_to(some_date)
b = date_to(some_date, "date")
c = date_to(some_date, str, timezone="JST")
d = date_to(some_date, to_type=int)

print(a, type(a))
print(b, type(b))
print(c, type(c))
print(d, type(d))

Output:

2001-09-11 21:20:00+00:00 <class 'datetime.datetime'>
2001-09-11 21:20:00+00:00 <class 'datetime.datetime'>
2001-09-12T06:20:00+09:00 <class 'str'>
1000243200 <class 'int'>

Accepted to_type Inputs

import datetime as dt

accepted_object_inputs = str | int | float | dt.datetime | dt.date
accepted_string_inputs = {
    "str": ["str", "string", "text", ],
    "int": ["int", "timestamp", "epoch", "unix", "float", ],
    "date": ["datetime.datetime", "datetime", "date", "dt", "dt.datetime", "dt.date", ],
}

Parse Settings

If you wish to change the string parse conversion behaviour you can add a dict of keyword arguments to the function's parser_settings= optional keyword argument. Please refer to the dateparser documentation for possible settings.

DEFAULT_SETTINGS = {
    "TIMEZONE": "UTC",
    "PREFER_DAY_OF_MONTH": "first",
    "RETURN_AS_TIMEZONE_AWARE": True,
}

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_to-1.2.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

date_to-1.2.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file date_to-1.2.1.tar.gz.

File metadata

  • Download URL: date_to-1.2.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for date_to-1.2.1.tar.gz
Algorithm Hash digest
SHA256 0f1e7eeff3f2164a072981fd117e415678c1c3242aae2e6cd8a9b5b6a965e992
MD5 d28699eb325a7e84d192f342765415cf
BLAKE2b-256 606901aa2aa0caaaf71bd9f328e1f37754ea1c376e71714b9401b50a2350fc8c

See more details on using hashes here.

File details

Details for the file date_to-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: date_to-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.1

File hashes

Hashes for date_to-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 540006223fb794ea680bde728aaa041b9bc8612c6330abc5adee3233f58eee8a
MD5 615dc298c1c3226eb71f29cf1ef044a2
BLAKE2b-256 8b808c86a848a2deef96851618a0d8ebd93e5ae8a96f034189cbc5cbf7d9fe59

See more details on using hashes here.

Supported by

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