Sari la conținut
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
    • Property Management
    • 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
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Toate postările Oameni Insigne
Etichete (Vezi tot)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Toate postările Oameni Insigne
Etichete (Vezi tot)
odoo accounting v14 pos v15
Despre acest forum
Suport

Datetime differnce

Abonare

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

Această întrebare a fost marcată
7 Răspunsuri
3619 Vizualizări
Imagine profil
Ali Mahmoud

Hi,

i'm trying to build a function that gives me the difference between two date-times, but it doesn't work and nothing changes when changing the dates

start_time = fields.Datetime(string='Start Time', default=fields.Datetime.now)
end_time = fields.Datetime(string='End Time', default=fields.Datetime.now)

@api.onchange('start_time', 'end_time')
def _get_time(self):
t1 = datetime.datetime.strptime(self.start_time, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.end_time, '%Y-%m-%d %H:%M:%S')
time_diff = (t2 - t1)
self.unit_amount = time_diff

can anyone tell me the wrong in my code?
Thanks
2
Imagine profil
Abandonează
Imagine profil
Ashish Singh
Cel mai bun răspuns

Hi, Ali


In Odoo 13 you don't need to use "strptime".

You can simply need subtract directly from fields. You can refer to the below code,

@api.onchange('start_time', 'end_time')
def _get_time(self):
    self.unit_amount = int(self.end_time - self.start_time)
For Odoo 12 you can use below code, (Updated My Answer for Odoo 12.0)
from datetime import datetime, timedelta
@api.onchange('start_time', 'end_time')
def _get_time(self):
    date_format = '%Y-%m-%d %H:%M:%S'
    t1 = datetime.strptime(str(self.start_time), date_format)
    t2 = datetime.strptime(str(self.end_time), date_format)
    time_diff = (t2 - t1)
    self.unit_amount = float(time_diff.seconds)
Thanks,
Ashish Singh (Team Lead)
Webkul Software Private Limited
6
Imagine profil
Abandonează
Ali Mahmoud
Autor

Thanks but I'm using Odoo 12

Ashish Singh

Hi, Ali

For Odoo 12 Kindly follow the below code,

from datetime import datetime, timedelta

@api.onchange('start_time', 'end_time')

def _get_time(self):

date_format = '%Y-%m-%d %H:%M:%S'

t1 = datetime.strptime(str(self.start_time), date_format)

t2 = datetime.strptime(str(self.end_time), date_format)

time_diff = (t2 - t1)

self.unit_amount = float(time_diff.seconds)

Thanks.

Ashish Singh

Ashish Singh

Hi, Ali

If field is float type then you needs to find total second,days etc. via timedelta object like code given my last post.

Imagine profil
Cybrosys Techno Solutions Pvt.Ltd
Cel mai bun răspuns

Hi,

I think you must pass a string instead of datetime when using strptime.Can you try after rewriting the code like below

@api.onchange('start_time', 'end_time')
def _get_time(self):
t1 = datetime.datetime.strptime(str(self.start_time), '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(str(self.end_time), '%Y-%m-%d %H:%M:%S')
time_diff = (t2 - t1)
self.unit_amount = time_diff

Regards

3
Imagine profil
Abandonează
Ali Mahmoud
Autor

Thanks

i got this error when i used str():

TypeError: float() argument must be a string or a number, not 'datetime.timedelta'

Cybrosys Techno Solutions Pvt.Ltd

Hi,

Please check your field type of unit_amount

Cybrosys Techno Solutions Pvt.Ltd

currently the time_diff will be of type datetime.timedelta

you can extract the required details from time_diff and allocate to unit_amount according to the field type of the unit_amount

Îți place discuția? Nu doar citi, alătură-te!

Creează-ți un cont astăzi pentru a beneficia de funcții exclusive și a interacționa cu minunata noastră comunitate!

Înscrie-te
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 Svenska ภาษาไทย 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