Skip to content

Instantly share code, notes, and snippets.

@rafnixg
Forked from ilyasProgrammer/date_compare.py
Created April 16, 2019 22:14
Show Gist options
  • Save rafnixg/746966ab09f1cdb5a547089cbfefa329 to your computer and use it in GitHub Desktop.
Save rafnixg/746966ab09f1cdb5a547089cbfefa329 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)]"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment