Odoo Help


Set default datetime at a specific hour

Martin Varela
on 11/4/13, 2:04 PM 5,523 views

I´m trying to set default date as the actual date at a specific hour, like today at 09:00

_defaults = {
    'date_start': VALUE,

i´ve tried: fields.date.context_today + datetime.timedelta(hours=9)
but throws an error because fields.date.context_today is a function.

If i only put: fields.date.context_today, displays today´s date at 00:00 but y want 09:00. How can i do this?

the field is datetime.

Martin Varela
on 11/4/13, 2:04 PM

_defaults={ 'date_start': lambda *a: time.strftime('%Y-%m-%d-00-00-00'), } will give you date in following format :2013-11-06 11:11:11 If you want other format you can change that

on 11/6/13, 3:41 AM

Thanks, that works, but it converts the time depending on the timezone. I had to create a function to convert set the time to the hour I wanted

Martin Varela
on 11/6/13, 10:37 AM

I think you will get exact time which is depending on tz so you need to use tz which present in context. I am not sure about this but hope it will help you :)

on 11/14/13, 2:45 AM
Miguel Neto
On 4/20/14, 2:09 PM

The solution for me was:

_defaults = {
    'date_start': lambda self,cr,uid,context=None: fields.date.context_today(self,cr,uid,context) + " 09:00:00"
Martin Varela
On 11/6/13, 12:14 PM

This is how I solved my problem, I couldn´t find another easier solution.

MVARELA - Function to set datetime independent of timezone

# set today´s date and hour/minute specified in the parameter horas with format 'hh:mm'
def x_calcular_fecha(self, cr, uid, context, horas):
    #get today´s date in utc, if I want 9hrs this converts to utc, for Uruguay (2hrs dif) 7hrs
    fecha_utc = datetime.strptime(time.strftime('%Y-%m-%d ' + horas), '%Y-%m-%d %H:%M')
    #convert the date again to my timezone 7hrs --> 5hrs
    fecha_zona = fields.datetime.context_timestamp(cr, uid, fecha_utc, context=context) 
    fecha_zona = fecha_zona.replace(tzinfo=None)
    # substract the time difference between utc and my timezone, 2hrs
    dif_horaria = fecha_utc - fecha_zona
    # return utc date + time difference  (7hrs + 2hrs --> 9hrs what I wanted)
    return str(fecha_utc + dif_horaria)

_defaults= {
    'date_start': lambda self, cr, uid, context={}: self.x_calcular_fecha(cr, uid, context, '09:00'),
    'date_end': lambda self, cr, uid, context={}: self.x_calcular_fecha(cr, uid, context, '18:00'),

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 11/4/13, 2:04 PM
Seen: 5523 times
Last updated: 3/16/15, 8:10 AM