past_date2word is library helps you to convert the past date to text
Use PyPi to install past_date2word.
pip3 install past_date2word
NOTE: Not all cases will be written because they are many, do not worry, you can see the tests file that contains all the possible cases (if you think otherwise, add the test and do a PR, we will be happy with that)
NOTE:
with_ago
parameter will add"ago"
to the end of the sentence if itsTrue
, and if not, he will not add it
NOTE:
long_sentence
parameter will add the hours, minutes and seconds to the sentence if itsTrue
, and if not, he will not add it
time = datetime.now() - timedelta(seconds=1)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "1 second" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "1 second ago" # Output of past_date2word
)
time = datetime.now() - timedelta(minutes=9, seconds=34)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=True)
== "9 minutes and 34 seconds ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "9 minutes and 34 seconds" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "9 minutes ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=False)
== "9 minutes" # Output of past_date2word
)
time = datetime.now() - timedelta(hours=12, minutes=44)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=True)
== "12 hours and 44 minutes ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "12 hours and 44 minutes" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "12 hours ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=False)
== "12 hours" # Output of past_date2word
)
time = datetime.now() - timedelta(days=5, hours=15)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=True)
== "5 days and 15 hours ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "5 days and 15 hours" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "5 days ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=False)
== "5 days" # Output of past_date2word
)
time = datetime.now() - timedelta(weeks=2, days=4)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=True)
== "2 weeks and 4 days ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "2 weeks and 4 days" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "2 weeks and 4 days ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=False)
== "2 weeks and 4 days" # Output of past_date2word
)
# 4 weeks == 1 month
one_month_to_week = 4 * 1
time = datetime.now() - timedelta(weeks=(one_month_to_week * 3) + 2)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=True)
== "3 months and 2 weeks ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "3 months and 2 weeks" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "3 months and 2 weeks ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=False)
== "3 months and 2 weeks" # Output of past_date2word
)
# 12 month == 1 year
# 4 weeks == 1 month
one_month_to_week = 4 * 1
one_year_to_week = one_month_to_week * 12
time = datetime.now() - timedelta(
weeks=(one_year_to_week * 12) + (one_month_to_week * 5)
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=True)
== "12 years and 5 months ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=True)
== "12 years and 5 months" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=True, long_sentence=False)
== "12 years and 5 months ago" # Output of past_date2word
)
assert (
past_date2word(date=time, language="en", with_ago=False, long_sentence=False)
== "12 years and 5 months" # Output of past_date2word
)
time = datetime.now() - timedelta(seconds=13)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 13 ثانية"
"13 ثانية"
"منذ 13 ثانية"
"13 ثانية"
time = datetime.now() - timedelta(minutes=13, seconds=13)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 13 دقيقة و 13 ثانية"
"13 دقيقة و 13 ثانية"
"منذ 13 دقيقة"
"13 دقيقة"
time = datetime.now() - timedelta(hours=13, minutes=13)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 13 ساعة و 13 دقيقة"
"13 ساعة و 13 دقيقة"
"منذ 13 ساعة"
"13 ساعة"
time = datetime.now() - timedelta(days=6, hours=15)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 6 ايام و 15 ساعة"
"6 ايام و 15 ساعة"
"منذ 6 ايام"
"6 ايام"
time = datetime.now() - timedelta(weeks=3, days=4)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 3 اسابيع و 4 ايام"
"3 اسابيع و 4 ايام"
"منذ 3 اسابيع و 4 ايام"
"3 اسابيع و 4 ايام"
one_month_to_week = 4 * 1
time = datetime.now() - timedelta(weeks=(one_month_to_week * 11) + 3)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 11 شهر و 3 اسابيع"
"11 شهر و 3 اسابيع"
"منذ 11 شهر و 3 اسابيع"
"11 شهر و 3 اسابيع"
# 12 month == 1 year
# 4 weeks == 1 month
one_month_to_week = 4 * 1
one_year_to_week = one_month_to_week * 12
time = datetime.now() - timedelta(
weeks=(one_year_to_week * 12) + (one_month_to_week * 5)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=True)
)
print(
past_date2word(date=time, language="ar", with_ago=True, long_sentence=False)
)
print(
past_date2word(date=time, language="ar", with_ago=False, long_sentence=False)
)
المخرجات
"منذ 12 سنة و 5 اشهر"
"12 سنة و 5 اشهر"
"منذ 12 سنة و 5 اشهر"
"12 سنة و 5 اشهر"