Javatpoint Logo
Javatpoint Logo

How to Humanize the Delorean Datetime Objects

In this tutorial, we will learn about the Python Delorean module, which is used to simplify the datetime object. We need a datetime module to work with different times and dates. Sometimes it can be a bit complicated and challenging, and handling datetime in Python is no exception. Python's built-in datetime module comes with various classes such as date, datetime, time, timezone and timedelta.

The Delorean library is built upon the pytz, a standard library for solving a problem related to timezones. We need to install it in our local system to work with this library.

Installation

Create, and activate the virtual environment and type the following command in the terminal.

Humanize Delorean Datetime Objects with humanize()

While Epoch timestamps are incredibly exact and great for digital devices - they could be more understandable by humans. We need reasoning alertness of such scales. Additionally, we only think of dates in some cases, except for some rare occasions. Most people might not have known what date it is, were it not for the constant clock and calendar built into most digital devices - from stationary to mobile.

Most of the time, we want to know the specific details regarding the given date, for example, when did we meet with a friend? On 15 August of 2022 or yesterday?

Python provides the humanize() function, which takes a date as an argument and turns it into what people would use while conveying the time of an event. Let's understand the following example -

Example -

Output:

25 years ago
24 years ago
24 years from now

This module provides an excellent feature for annotating the time before and after the event. For example - We can inform the users to receive a notification or a birthday is coming next month, and we can show the exact timing if the event is N days older.

We can switch N days older events using the timedelta. We will measure if a date has occurred more than N days before the current date. Let's understand the following example -

Example -

Output:

Event 0 has occurred on 1997-11-13
Event 1 has occurred on 2015-12-02

The above output is more precise and human readable form, we can say that a specific event has happened on some specific date.

Humanize Relative Dates with Delorean

Delorean also provides the facility to work with the dates in relation to another date, saving lots of time and making developer life more straightforward than using time delta. Using the combination of the next_ and last_ with days of the week, month or even years, we can calculate and create Delorean objects. Let's understand the following example.

Example -

Output:

Friday next week:  Delorean(datetime=datetime.datetime(2022, 10, 28, 14, 20, 31, 261138), timezone='UTC')
Friday two weeks ago:  Delorean(datetime=datetime.datetime(2022, 10, 14, 14, 20, 31, 261138), timezone='UTC')
Two Mondays from now at midnight:  2022-11-07 00:00:00+00:00
This time next month:  Delorean(datetime=datetime.datetime(2022, 11, 25, 14, 20, 31, 261138), timezone='UTC')
This time next month:  2023-10-25 00:00:00+00:00

Conclusion

This tutorial included the Delorean module in Python and humanizing Delorean datetime objects in Python. Humanize date is a term for converting the datetime instance to a more human format - using words and relative timeframes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA