0

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
mikegr3p
  • 23
  • 5
  • 1
    You'll want to compare datetime objects. So you'll have to convert the ISO format string to that, see e.g. https://stackoverflow.com/q/127803/10197418 – FObersteiner Jul 03 '22 at 17:25

2 Answers2

0

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

mzvic
  • 28
  • 4
  • If I converted to string how can I compare it as a date I need to know if date is greater then another date – mikegr3p Jul 03 '22 at 17:35
0

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

shivarama23
  • 217
  • 1
  • 6
  • 1
    You probably don't want to ignore the Z - that's UTC while datetime.today returns naive datetime, thus *local* time. – FObersteiner Jul 03 '22 at 18:11
  • Since `date_formatted` and `minimumDate` are both `datetime` objects they can be compared directly — i.e. `if date_formatted > minimumDate:`. I also suggest using the third-party `dateutil` module which will parse the timezone properly. It's easy to install from the command line via: `pip install python-dateutil`. – martineau Jul 03 '22 at 18:29