Shortening Standard Date & Time Formats to Make Them Easier to Understand
source link: https://hackernoon.com/python-solutions-for-clear-and-human-friendly-dates-and-times
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Shortening Standard Date & Time Formats to Make Them Easier to Understand
4 min
by @robmccormack
Rob McCormack
@robmccormack
Engineer / Professor (retired) I worked for years as a...
Too Long; Didn't Read
This article will show you how to shorten Python's standard date/time formats to "1 month ago", or "Just now". YouTube uses this for video publication dates. It's easy.audio
element.Imagine if your favorite YouTube video displayed a publication date like 2017-10-07-14:55:09
. Dates in this format require mental effort to understand. Have you ever misunderstood reading such dates by misjudging the year or month, or being off 2 hours due to the 24-hour format? This article will show you how to shorten standard date/time formats to 5 years ago
or Just now
. It would be hard to argue that this method isn't superior.
ChatGPT Assistance
I wrote this script Python 3 script as a learning exercise. I employed ChatGPT to help with not only the programming but also the crafting of this article. I was surprised at how useful it was. (ChatGPT is a natural language processing tool driven by AI technology). ChatGPT seemed happy to write the introduction and the conclusion for this article. When I asked ChatGPT to write the entire article it replied: "I'm sorry that goes beyond my current capabilities." Oddly, it didn't complain when I requested additional paragraphs - which made me laugh. It turned out I had to write a lot of the article from scratch.
The Solution
Python will format the date to more readable formats like January 30, 2022 14:55:09
- a whopping 25 characters long. That helps a little bit with readability. This article will show you how to print dates as: 1 year ago
or 1 month ago
. If less than a minute ago then Just now
will be printed. This is the same format that YouTube uses for time-stamping content.
Function Breakdown
The Python script consists of three main functions:
calculate_time_difference(date_input)
takes a date string as input and returns the difference between that date and the current time as adatetime.timedelta
object.convert_time_difference_to_string(time_difference)
converts thetimedelta
object into a human-readable string showing how much time has passed since the input date. This function work by looping through thetime_units
usingfor
loop, starting with the largesttime_unit
(year) and checking with thecurrent time_difference
, if it's greater than or equal to this unit it returns the string with the number ofunit
andtime_units
, and then break out of the loop. The function also includes a check for returningJust now
if the time difference is less than a minute.get_how_long_ago(date_input)
combines the first two functions and handles any errors that might occur, such as invalid date formats or dates in the future.
You can change the date format of %Y-%m-%d %H:%M:%S
to suit your needs.
The last part of the script prints out examples of dates processed by get_how_long_ago
. I've used now()
to generate some of the examples. Cases for invalid dates are also provided.
Full Script
You can customize this script to your own needs. For mobile devices, YouTube will often abbreviate 1 year ago
to 1y ago
. If you prefer that, simply change the year
to y
and comment out the test for pluralization as shown:
if num_units > 1:
time_string += 's'
Going Further
Dates in Tables
It may be useful when presenting dates in a table to use the how long ago
formatting. For example, compare the presentation of sample data sorted by Last_Visit
field:
-------------------------------------
ID Dog Name Last Visit
1 Bella-boy 2023-01-08
3 Charlie 2022-07-01
2 Greta 2018-10-10
Could be presented like this:
-------------------------------------
ID Dog Name Last Visit
1 Bella-boy 3 days ago
3 Charlie 6 months ago
2 Greta 4 years ago
Provide Both Types of Formats
YouTube will first present you with 5 years ago
but if you click or hover over that text, the exact date will be displayed, Oct 6, 2017
. The function could return a tuple
containing both formats. This may be a nice feature to consider in your programs that involve dates.
Conclusion
While this script provides a straightforward and effective solution for converting date strings into a more human-readable format. You may want to consider using the humanize
Python package for even more advanced formatting options. Docs can be found here.
I hope you enjoy playing around with this technique in your projects.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK