简体   繁体   中英

Convert epoch to human-readable date python

I want to convert from epoch to date like that: 1575135888 ==> 3 years ago ( https://i.stack.imgur.com/x2ZPW.png )

I have this code:

date_c = datetime.datetime.fromtimestamp(1575135888)
print(datetime.date.today())  
print(date_c) 

I got 2022-12-09

and 2019-11-30 18:44:48

I don't know how to convert it to 3 years ago.

Well, you could use timedelta like this:

from datetime import datetime
print(datetime.today() - datetime.fromtimestamp(1575135888), "ago")

Output:

1104 days, 15:57:21.760720 ago

But I'm afraid that the meaning of "years ago" is ambiguous: Does it mean 365 days, 365.25 days?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM