Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

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

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

[Solved] Date/ Time in Odoo with python

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
pythondatetime
8 Replies
63039 Tampilan
Avatar
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
Avatar
Buang
Avatar
Laza Andriamanantena
Jawaban Terbai

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
Avatar
Buang
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

Avatar
Temur
Jawaban Terbai

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
Avatar
Buang
Siobhan
Penulis

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
Penulis

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

Siobhan
Penulis

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
Penulis

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

Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
DateTime Python Diselesaikan
python datetime
Avatar
Avatar
Avatar
Avatar
4
Feb 23
72999
Difference Between Two Dates (result not showing up when diff = 0 Day)
datetime
Avatar
Avatar
Avatar
3
Jun 25
1770
new python env
python
Avatar
0
Mar 25
2280
What means "Too many values to unpack" message? Diselesaikan
python
Avatar
Avatar
Avatar
Avatar
Avatar
4
Apr 24
175736
have no data in screen. read data in my own module from different model
python
Avatar
0
Des 23
2934
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة 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 adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik Odoo.

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