Created
July 23, 2024 20:37
-
-
Save Pymmdrza/461bbc56406e8a5fed4f93d3bb28b170 to your computer and use it in GitHub Desktop.
Time Last Date Ago On Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
def format_time_ago(download_date): | |
download_datetime = datetime.strptime(download_date, '%Y-%m-%d %H:%M:%S') | |
current_datetime = datetime.now() | |
time_difference = current_datetime - download_datetime | |
if time_difference.days > 365: | |
years = time_difference.days // 365 | |
return f"{years} Year ago" | |
elif time_difference.days > 30: | |
months = time_difference.days // 30 | |
return f"{months} Month ago" | |
elif time_difference.days > 0: | |
return f"{time_difference.days} Day ago" | |
elif time_difference.seconds // 3600 > 0: | |
hours = time_difference.seconds // 3600 | |
return f"{hours} Hour ago" | |
elif time_difference.seconds // 60 > 0: | |
minutes = time_difference.seconds // 60 | |
return f"{minutes} Min ago" | |
else: | |
return "Just now" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment