๐
Work with datetime
python
from datetime import datetimeimport timedt1 = datetime(2018,1,1)dt2 = datetime.now()dt3 = datetime.strptime("2018/01/01", "%Y/%m/%d") # convert string to datetime objectdt4 = datetime.fromtimestamp(time.time())print(f"{dt4.year}/{dt4.month}")print(dt4.strftime("%Y/%m")) # convert object to the stringsprint(dt2 > dt1)
Using timedelta module
python
from datetime import datetime, timedeltaimport timedt1 = datetime(2018, 1, 1)print(dt1)dt1 += timedelta(days=1, seconds=200)print(dt1)dt2 = datetime.now()duration = dt2 - dt1print("duration: ", duration)print("Days: ", duration.days)print("Seconds: ", duration.seconds)print("Total Second: ", duration.total_seconds())
Output
2018-01-01 00:00:002018-01-02 00:03:20duration: 957 days, 16:30:23.594142Days: 957Seconds: 59423Total Second: 82744223.594142