Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    Retail
    • Book Store
    • Magazin de îmbrăcăminte
    • Magazin de Mobilă
    • Magazin alimentar
    • Magazin de materiale de construcții
    • Magazin de jucării
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Distribuitor de băuturi
    • Hotel
    Proprietate imobiliara
    • Real Estate Agency
    • Firmă de Arhitectură
    • Construcție
    • Estate Managament
    • Grădinărit
    • Asociația Proprietarilor de Proprietăți
    Consultanta
    • Firma de Contabilitate
    • Partener Odoo
    • Agenție de marketing
    • Law firm
    • Atragere de talente
    • Audit & Certification
    Producție
    • Textil
    • Metal
    • Mobilier
    • Mâncare
    • Brewery
    • Cadouri corporate
    Health & Fitness
    • Club Sportiv
    • Magazin de ochelari
    • Centru de Fitness
    • Wellness Practitioners
    • Farmacie
    • Salon de coafură
    Trades
    • Handyman
    • IT Hardware and Support
    • Asigurare socială de stat
    • Cizmar
    • Servicii de curățenie
    • HVAC Services
    Altele
    • Organizație nonprofit
    • Agenție de Mediu
    • Închiriere panouri publicitare
    • Fotografie
    • Închiriere biciclete
    • Asigurare socială
    Browse all Industries
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

[Solved] Date/ Time in Odoo with python

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
pythondatetime
8 Răspunsuri
63044 Vizualizări
Imagine profil
Siobhan

I have an onchange class written :


@api.onchange('res_users_id')

def do_stuff(self):

if self.res_users_id:

self.beschreibung = str(now) + '\n' + str(self.beschreibung)

else:

self.beschreibung = ' '


and a global variable: now = datetime.datetime.now()

I have 2 problems:

1: it is showing about 2 hours behind my current time, how do I fix that? and

2: how do I get rid of the milliseconds at the end - 10:07:15.471708?


4
Imagine profil
Abandonează
Imagine profil
Laza Andriamanantena
Cel mai bun răspuns

Hi, I know this post is already solved but I has the same issues on Odoo 11, 12 and in my case I use fields.Datetime.context_timestamp , like this:
​

now= datetime.strftime(fields.Datetime.context_timestamp(self, datetime.now()), "%Y-%m-%d %H:%M:%S")

you need to declare it in on methode on models.Models, because the self take the timezone of the current user


5
Imagine profil
Abandonează
CARLOS ALBERTO GARCIA BRIZUELA

For me works great in Odoo v13 as well !!

Nayar Rodriguez

Thank you so much, works greaaaat! Odoo V13 as well

Imagine profil
Temur
Cel mai bun răspuns

1. I guess you're in UTC+2 time zone? Take in account that server time is in UTC, but that's corrected according timezone of user (timezone of users is configured in settings), so in user interface  every user see time correctly in it's timezone (that's nice solution for a case if several user are logged into the same server from different timezone) consider to use datetime and date fields (also try widget="datetime" widget="date"), finally users will see correct time.

2. consider to use strftime / strptime play.


UPDATE for 1.

if you want add or subtract that 2 hours directly at server side (in python), then you can use (but take in account that it may show wrong time to users in browser, because of what I posted above...):

from datetime import timedelta

...

now = datetime.datetime.now() + timedelta(hours=2) 

...


UPDATE: one more option to get time in user timezone at serverside:

in your case, you may find useful to use server_to_local_timestamp function from openerp.tools.misc

from openerp import tools
...

src_tstamp_str = tools.datetime.now().strftime(tools.misc.DEFAULT_SERVER_DATETIME_FORMAT)

src_format = tools.misc.DEFAULT_SERVER_DATETIME_FORMAT

dst_format = "%c" #format you want to get time in.

dst_tz_name = self.env.user.tz # timezone you want get time in (here used v8 api, otherwise you'll have to browse for user and access tz field using "user_browserecord.tz")

_now = tools.misc.server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name)

note: user should have configured a timezone in settings. if that's not a case, then odoo displays warning (!) at upper right corner in UI in browser.
8
Imagine profil
Abandonează
Siobhan
Autor

Thank you for the information... I am trying to use %c Locale’s appropriate date and time representation. but it doesn't like the syntax... what is the correct way to use this?

Siobhan
Autor

Is there a way to use timedelta in this piece of code: now = strftime("%a, %d %b %Y %H:%M:%S ", localtime())

Siobhan
Autor

Ok.. got it down to one line of code: now = datetime.datetime.now().strftime("%c") Problem is that I am again 2 hours out even when my time preference in Odoo is set to Central European time.

Temur

you can turn your code into the:

now = ( datetime.datetime.now() + timedelta(hours=2) ).strftime("%c")
this way you can add back that 2 hours difference to the current time. also consider to use another option that I've added to my answer, choose one which better fits your requirements.
Siobhan
Autor

Thank you Temur... timedelta is perfect for now as everyone is in Central Europe so I don't need timezone. Will look at the code above and try get it to work during my quiet time.

CARLOS ALBERTO GARCIA BRIZUELA

Thanks, you are the best, exactly what I need! Cheers

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
DateTime Python Rezolvat
python datetime
Imagine profil
Imagine profil
Imagine profil
Imagine profil
4
feb. 23
73000
Difference Between Two Dates (result not showing up when diff = 0 Day)
datetime
Imagine profil
Imagine profil
Imagine profil
3
iun. 25
1773
new python env
python
Imagine profil
0
mar. 25
2284
What means "Too many values to unpack" message? Rezolvat
python
Imagine profil
Imagine profil
Imagine profil
Imagine profil
Imagine profil
4
apr. 24
175738
have no data in screen. read data in my own module from different model
python
Imagine profil
0
dec. 23
2934
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now