ZEQUET
ZEQUET

Reputation: 51

Python Dates Format

Hi I am trying to subtract two dates, one coming from MongoBd as date format and the other as DateTime.now() on Python. The format is not the one I need. I would like to show just the day but instead, it is showing this format: "1 day, 9:06:17.913564 " but I would like to show just this "1 day" Any feedback will be appreciated.

date coming from Mongo as a date format

enter code here

 mongo_date = 2021-12-31T06:00:00.000+00:00 
 todayday = datetime.now()
 duedate = mongo_date - todayday
 

duedate= 1 day, 9:06:17.913564

I would like to show the duedate as "1 day"

Upvotes: 1

Views: 117

Answers (1)

BioData41
BioData41

Reputation: 992

How about this: (Simplified based on @MrFuppes comment avoiding pytz import.)

import datetime

mongo_date = "2021-12-31T06:00:00.000+00:00" 
mongo_date = datetime.datetime.fromisoformat(mongo_date)

#Since your mongo_date is in UTC time zone, I'm using UTC for todayday:
todayday = datetime.datetime.now(datetime.timezone.utc)

duedate = mongo_date - todayday #this is a timedelta object
print("Due date is {} days".format(duedate.days))

Upvotes: 1

Related Questions