Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Sociale media-marketing
    • E-mailmarketing
    • Sms-marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

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

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

how to calculate difference bitween two dates ?

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
17 Antwoorden
54187 Weergaven
Avatar
jean44

Hi, in my object I have two date fields: date1:fields.date('date 1'), date2:fields.date('date 2'),

I want to retrieve the difference of that 2 fields I tried this : date2 - date1 but I got this error : TypeError: unsupported operand type(s) for -: 'str' and 'str'

3
Avatar
Annuleer
OpenERP Vietnam

to do this, the first you must convert into date format (use lib datetime: datetime.date(year, month, day)). Then you can use operator '-' in this case. Refer: http://docs.python.org/2/library/datetime.html

jean44
Auteur

thank you !

Avatar
nishad
Beste antwoord

Hi mos, You need to do some python level stuffs with dates for getting exact date difference , i will explain with an example

from datetime import datetime

fmt = '%Y-%m-%d'
from_date = '2013-09-01'     -> Take your date field1
to_date = '2013-09-31'       -> Take your date field2

d1 = datetime.strptime(from_date, fmt)

d2 = datetime.strptime(to_date, fmt)

daysDiff = str((d2-d1).days)          -> will gives you 29 days

daysDiff = str((d2-d1).days + 1)      -> will gives you 30 days

Hope it helps you ........ Thanks

8
Avatar
Annuleer
jean44
Auteur

thank you !

Atul Kumar jain

hi nishad thanks for your answer i want to calculate it automatically and show in readonly=True field so can you please provide me some extra detail for calculate duration

nishad

Define two date fields(from date , to date) & one functional field(difference_days) to keep the difference days. Do implement the date difference calculation steps in your function and return the value . A functional field in openerp will return a readonly = True value by default. If you want to save data in to DB then use store=True parameter while defining functional field.

Atul Kumar jain

my field is 'date_s':fields.datetime('Start Date'), 'date_e':fields.datetime('End Date'), 'duration':fields.function(_get_days, string='Duration', store=True), and my function is this def _get_days(self, cr, uid, ids, field_name, args, context=None): res = {} for date in self.browse(cr, uid, ids, context=context): res[date.id] = ((date.date_s-date.date_e).days) or False return res but it give me error like this res[date.id] = ((date.date_s-date.date_e).days) or False TypeError: unsupport

nishad

You need to include 'from datetime import datetime' in the import section and then do a date formatting using 'datetime.strptime(from_date, fmt)' for each date fields where fmt = '%Y-%m-%d' , because we are doing a conversion from openerp datetime format to native python format and then calculating the difference . Do one by one as i shown in my source code instead of doing it in single step.

Atul Kumar jain

i correct my function but still i got error like this data_string[found.end():]) ValueError: unconverted data remains: 10:52:57 and my function is def _get_days(self, cr, uid, ids, field_name, args, context=None): res = {} for date in self.browse(cr, uid, ids, context=context): fmt = '%Y-%m-%d' date.date_s = datetime.strptime(date.date_s, fmt) date.date_e = datetime.strptime(date.date_e, fmt) res[date.id] = ((date.date_s-date.date_e).days) or False return res

nishad

This is because your fields are of type' datetime' , instead use normal 'date' in openerp.

Atul Kumar jain

i already do that but still got erro

Atul Kumar jain

hi nishad please help me

nishad

def _get_days(self,cr,uid,ids,field_name, arg ,context=None):
res = {}, fmt = '%Y-%m-%d', for object in self.browse(cr, uid, ids, context=context): res[object.id] = {'diff_days':0, } from_date = object.date_a, to_date = object.date_b, d1 = datetime.strptime(from_date, fmt), d2 = datetime.strptime(to_date, fmt) ,daysDiff = str((d2-d1).days), res[object.id]['diff_days'] = daysDiff , return res

nishad

'date_a':fields.date("Date1"), 'date_b':fields.date("Date2"), 'diff_days':fields.function(_get_days,string="Diff days", multi='sums',store=True),

Atul Kumar jain

Hi nishad thanks for your reply i solve this problem

Tochukwu Eze

Hi
I am got this error (TypeError: unsupported operand type(s) for -: 'str' and 'str')when I tried out the above code.
Please can anyone help me out.
Thanks

Avatar
Silverstar
Beste antwoord

Hi,

This coluld be helpful.


date = fields.Date.from_string(old_date)

 worked_days = (fields.date.today() - date).days


Regards,

1
Avatar
Annuleer
Tochukwu Eze

Hi Atul Kumar jain
Please how did you solve the above because I am having the same issue

Silverstar
Hi,

What is the issue are you facing?

On Fri, 24 Sep 2021, 5:31 pm Tochukwu Eze, <tochukwu.eze@primespectrum.com> wrote:

Hi Atul Kumar jain
Please how did you solve the above because I am having the same issue

Sent by Odoo S.A. using Odoo.

Avatar
zahid
Beste antwoord

Use timedelta object form python

from datetime import timedelta

https://docs.python.org/2/library/datetime.html#timedelta-objects

0
Avatar
Annuleer
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة 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 is een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

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