Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
3827 Vizualizări

Hello,


I have this piece of code to fill a text field with a time:


MyDate = record.x_studio_approved_date_time_exit + datetime.timedelta(hours=1)


As you see, I hard coded the time difference with odoo server. But I would like to remove that hard code and make it work all year long (and not modify hours to 1 or 2 depending on summer or winter).

Could you tell me how I must change this to always show my time in CET?


Thank you for your help!

Imagine profil
Abandonează
Autor Cel mai bun răspuns

Thank you, but I need additional precision.

My answer is probably in:

user_tz = self.env.user.tz
localutc = pytz.timezone('UTC')
to_usertz = pytz.timezone(user_tz) or pytz.utc

So is it correct to do it like below:

original hard coding:

MyDate = record.x_studio_approved_date_time_exit + datetime.timedelta(hours=2)
record['x_studio_time_exit'] = MyDate.strftime("%H:%M")

Modified code:

user_tz = self.env.user.tz
MyDate = record.x_studio_approved_date_time_exit.user_tz
record['x_studio_time_exit'] = MyDate.strftime("%H:%M")


But it gives me an error...




Imagine profil
Abandonează
Cel mai bun răspuns

Hi,

We can get the time based on the timezone 

Try this, 

Using this in Jquery

var date = moment.utc("Value").local();
var formatted_date = date.format('YYYY-MM-DD HH:mm:ss');

Using this in Python

user_tz = self.env.user.tz
localutc = pytz.timezone('UTC')
to_usertz = pytz.timezone(user_tz) or pytz.utc

OR

time = fields.Datetime.from_string("Value")
formatted_time = (time + timedelta(hours=1)).strftime('%Y-%m-%d %H:%M:%S')

Regards

Imagine profil
Abandonează