I want to get the month and year from the field date in odoo 13. But I got this error :
TypeError: strptime() argument 1 must be str, not datetime.date
Here is my code:
@api.depends('date_from')
def _compute_month(self):
for rec in self:
date_from = datetime.strptime(rec.date_from, "%Y-%m-%d").date()
rec.month = date_from.month
rec.year = date_from.year
Solution: Try to change the following line as below:
date_from = datetime.strptime(str(rec.date_from), "%Y-%m-%d").date()
Note: Remember one thing, when we use: from datetime import datetime
then use this: date_from = datetime.strptime(str(rec.date_from), "%Y-%m-%d").date()
But when we use only: import datetime
Then use: date_from = datetime.datetime.strptime(str(rec.date_from), "%Y-%m-%d").date()
Hope it will help. Thanks.
Solution: Try to change the following line as below:
date_from = datetime.strptime(str(rec.date_from), "%Y-%m-%d").date()
Note: Remember one thing, when we use: from datetime import datetime
then use this: date_from = datetime.strptime(str(rec.date_from), "%Y-%m-%d").date()
But when we use only: import datetime
Then use: date_from = datetime.datetime.strptime(str(rec.date_from), "%Y-%m-%d").date()
Hope it will help. Thanks.