1

I have a user's server join date, I want to see if their join date is over a month ago. If it is, continue.

today = datetime.datetime.now()
lastMonth = today - datetime.timedelta(days=1)
if user.joined_at > lastMonth:
    print(user.joined_at)

The error it gives me.

if user.joined_at > lastMonth:
TypeError: can't compare offset-naive and offset-aware datetimes`
Flewitt
  • 76
  • 9

1 Answers1

0

If by a month you mean 30 days, then you can do something like that:

import datetime

now = datetime.datetime.utcnow()
one_month_delta = datetime.timedelta(days=30)

if now - time_to_check > one_month_delta:
    # A month has passed

If you want to test if a calendar month has passed, then you would have to do a little more work and take into consideration that not all months contain the same amount of days...

Amit Sides
  • 274
  • 2
  • 6
  • I just saw your edit. In your case, you need to replace `time_to_check` with `user.joined_at` – Amit Sides Jan 28 '22 at 22:53
  • Also, if `user.joined_at` is a utc time, then the above should work fine. If it's not, you need to use `datetime.now()` instead of `datetime.utcnow()` – Amit Sides Jan 28 '22 at 22:56