Skip to main content

A handy python function to parse and convert to and between datetime.datetime, int, and string objects

Project description

date_to - The easy date converter

PyPI - Python Version GitHub PyPI

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 conversion is to the UTC timezone. As of now, timezone conversion is only supported from str to str | datetime.date. Further conversion support is planned.

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, int)
d = date_to(some_date, str, timezone="JST")

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'>
1000243200 <class 'int'>
2001-09-12T06:20:00+09:00 <class 'str'>

Accepted Inputs

from datetime import date

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

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.0.tar.gz (11.2 kB view hashes)

Uploaded Source

Built Distribution

date_to-1.0-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

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