-
对于日期(date)和时间字段(datetime)odoo自带两个方法获取当前的日期和时间
日期字段默认为今天default=fields.Date.context_today
时间字段默认为现在default=fields.Datetime.now
-
对于在xml中进行时间段的筛选,odoo集成了dateuti中的relativedelta方法
在xml中同样可以使用context_today方法获取当天日期,再加上相差时间即可获取任意时间段- 获取上周周一可以写成
context_today()+relativedelta(weeks=-2,days=1, weekday=0)
weekday指的是计算完日期后第一个星期几(weekday指定0-6为周一至周日的索引)
上面代码意思为:当前时间减2周加1天之后的第一个周一,为什么这样可以保证是上周一自己琢磨一下就知道了
上周日就很简单了:
context_today()+relativedelta(weeks=-1,weekday=6)
计算完时间后要strftime转出字符串作为domain条件
应用在xml中编写search条件
- 获取上周周一可以写成
<search>
<filter string="上周收款" name="last_week"
domain="[('date', '>=', ((context_today()+relativedelta(weeks=-2, days=1, weekday=0)).strftime('%%Y-%%m-%%d'))),
('date', '<=',((context_today()+relativedelta(weeks=-1,weekday=6)).strftime('%%Y-%%m-%%d')))]"/>
</search>
筛选上月就是小于本月1号,大于等于上月1号
<filter string=" 上月收款" name="last_month"
domain="[('date','<', time.strftime('%Y-%m-01 00:00:00')),
('date','>=', (context_today() - relativedelta(months=1)).strftime('%Y-%m-01 00:00:00'))]"/>
- 再说说在pgsql中用sql语句查询上周
select * from move_line
where
date >= current_date +cast(-1*(TO_NUMBER(to_char(DATE (current_date),'D'),'99')-2) ||' days' as interval) - interval '7 day'
and
date < current_date +cast(-1*(TO_NUMBER(to_char(DATE (current_date),'D'),'99')-2) ||' days' as interval)
- 查询上月
select * from move_line
where
date >= to_timestamp(substring(to_char(now(),'yyyy-MM-dd hh24:MI:ss') FROM 1 FOR 10),'yyyy-MM-dd')- interval '1 month'
and
date_trunc('month',current_date)
网友评论