Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
3 ตอบกลับ
3243 มุมมอง

In odoo, field "createDate" has timezone by user, but when I print, it UTC.


I want to print timezone by browser user.


Please help me

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi BB,

Based on my idea you can use,

fields.Datetime.context_timestamp()

But based on that

import pytz

user_tz = self.env.user.tz or 'UTC'  # Get user's timezone or use UTC as default
tz = pytz.timezone(user_tz)
today_with_tz = fields.Date.to_string(
fields.Datetime.context_timestamp(self, fields.Datetime.from_string(today)).astimezone(tz))

i already use my work


Thanks



อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi,

Try fields.Datetime.context_timestamp(record, timestamp) method. It returns the given timestamp converted to the client’s timezone.


Hope it helps

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi 

Try the following code

import pytz

user = self.env['res.users'].browse(self.env.uid)
tz = pytz.timezone(user.tz if user.tz else 'UTC')
local_date = pytz.utc.localize(create_date).astimezone(tz)

where create_date = self.create_date 

output is like this 2023-12-31 14:23:19+03:00 

if you need to remove the offset 

date_time_without_offset = local_date.replace(tzinfo=None)


Regards

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ม.ค. 24
1484
1
ก.พ. 22
4599
0
มี.ค. 15
5740
2
มี.ค. 25
1834
2
เม.ย. 23
3244