Skip to content

Instantly share code, notes, and snippets.

@ilyasProgrammer
Last active January 29, 2023 08:07
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save ilyasProgrammer/b2c6e78716c0e5286caa7c1184f82d0d to your computer and use it in GitHub Desktop.
Save ilyasProgrammer/b2c6e78716c0e5286caa7c1184f82d0d to your computer and use it in GitHub Desktop.
Odoo compare datetime and date in domain. Current day, week, month, year in domain.
# in xml:
# Current day:
domain="[('some_date_time', '>', (context_today()-datetime.timedelta(days=1)).strftime('%%Y-%%m-%%d')),('start_time', '<', (context_today()+datetime.timedelta(days=1)).strftime('%%Y-%%m-%%d'))]
# Current week:
domain="[('some_date_time', '>=', (context_today()-datetime.timedelta(days=context_today().weekday())).strftime('%%Y-%%m-%%d')),('start_time', '<=', (context_today()-datetime.timedelta(days=context_today().weekday())+ datetime.timedelta(days=6)).strftime('%%Y-%%m-%%d'))]
# Current year:
domain="[('some_date_time', '>=', (datetime.date(datetime.date.today().year, 1, 1)).strftime('%%Y-%%m-%%d')),('start_time', '<=', (datetime.date(datetime.date.today().year, 12, 31)).strftime('%%Y-%%m-%%d'))]
#in python:
yesterday = datetime.datetime.now() - datetime.timedelta(days = 2)
yesterday_beginning = datetime.datetime(yesterday.year, yesterday.month, yesterday.day,0,0,0,0)
yb = yesterday_beginning.strftime("%Y-%m-%d %I:%M:%S")
today = datetime.datetime.now()
today_beginning = datetime.datetime(today.year, today.month, today.day,0,0,0,0)
tb = today_beginning.strftime("%Y-%m-%d %I:%M:%S")
self.env['res.users'].search([('write_date','>=',yb),('write_date','<',tb)])
# Current mont:
# I did not found decision without python
import calendar
import datetime
is_current_month = fields.Boolean(compute="_check_current_month", store=True)
@api.depends('start_time')
def _check_current_month(self):
for rec in self:
first_day = datetime.date.today().replace(day=1)
last_day = datetime.date.today().replace(day=calendar.monthrange(datetime.date.today().year, datetime.date.today().month)[1])
cur_date = datetime.datetime.strptime(rec.start_time, '%Y-%m-%d %H:%M:%S').date()
if first_day <= cur_date <= last_day:
rec.is_current_month = True
else:
rec.is_current_month = False
<field name="is_current_month" invisible="1"/>
<filter string="Current month" name="month" domain="[('is_current_month','=', True)]"/>
@em230418
Copy link

Спасибо!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment