Home > Blockchain >  Comparing two dates in different formats
Comparing two dates in different formats

Time:07-04

I have two dates, one is a string in following format,

"2022-07-03T12:23:49.000Z"

The other is a datetime object from this:

minimumDate = datetime.today() - timedelta(days=10) 

How can I format them so I can compare them both?

P.S. I am not familiar with python, just use it for a project.

CodePudding user response:

You can convert your format of date to datetime object using the below:

date_str = "2022-07-03T12:23:49.000Z"
date_formatted = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ")

Then both the formatted date(date_formatted) and minimumDate will be in datetime format to compare, as shown below.

minimumDate - date_formatted

Below is the reference for the answer: Convert custom strings to datetime format

CodePudding user response:

The variable minimumDate is type 'datetime.datetime' to compare both you need to convert it in a string

minimumDate = str(datetime.today() - timedelta(days=10))

That way both are strings

  • Related