odoo10 中的日期时间问题?
Datetime issue in odoo10?
在我的自定义模型中,我定义了字段
time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
tot_time = fields.Char("Time Difference", compute='_get_time')
这是我的计算函数
@api.depends('time_from', 'time_to')
def _get_time(self):
t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
if t2<t1:
raise ValidationError('Time To must greater than Time From')
time_diff = (t2-t1)
self.tot_time = time_diff
这是成功完全打印时差。
从时间到时间都是mm/dd/yyyyhh:mm:ss格式。
如何将其更改为 mm/dd/yyyy hh:mm:ss 格式
我像这样更改了格式 '%Y-%d-%m %H:%M:%S' 。
但它没有得到正确的结果。
如何更改格式?
这种格式可以计算时差吗?
Date
和Datetime
在数据库中以一定格式保存为字符串(参见fields.py
上的class定义)。您不能更改用于 ORM 中的字段。如果你想在显示这些字段时更改日期或日期时间的格式,你不能从代码中更改,而是从:
1) 设置 -> 翻译 -> 找到您的语言,您可以在其中更改日期和日期时间字段在客户端的呈现方式。
2) 如果您有 template/report,您可以使用例如 <p t-esc="formatLang(time.strftime('%Y-%m-%d %H:%M:%S')" />
或您想要更改日期或日期时间形成方式的其他表达式。
3) 在 xml 文件的字段定义中,您可以使用自定义 javascript/widget 进行渲染。
在我的自定义模型中,我定义了字段
time_from = fields.Datetime(string="Time From", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
time_to = fields.Datetime(string="Time To", default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
tot_time = fields.Char("Time Difference", compute='_get_time')
这是我的计算函数
@api.depends('time_from', 'time_to')
def _get_time(self):
t1 = datetime.datetime.strptime(self.time_from, '%Y-%m-%d %H:%M:%S')
t2 = datetime.datetime.strptime(self.time_to, '%Y-%m-%d %H:%M:%S')
if t2<t1:
raise ValidationError('Time To must greater than Time From')
time_diff = (t2-t1)
self.tot_time = time_diff
这是成功完全打印时差。 从时间到时间都是mm/dd/yyyyhh:mm:ss格式。
如何将其更改为 mm/dd/yyyy hh:mm:ss 格式
我像这样更改了格式 '%Y-%d-%m %H:%M:%S' 。 但它没有得到正确的结果。
如何更改格式?
这种格式可以计算时差吗?
Date
和Datetime
在数据库中以一定格式保存为字符串(参见fields.py
上的class定义)。您不能更改用于 ORM 中的字段。如果你想在显示这些字段时更改日期或日期时间的格式,你不能从代码中更改,而是从:
1) 设置 -> 翻译 -> 找到您的语言,您可以在其中更改日期和日期时间字段在客户端的呈现方式。
2) 如果您有 template/report,您可以使用例如 <p t-esc="formatLang(time.strftime('%Y-%m-%d %H:%M:%S')" />
或您想要更改日期或日期时间形成方式的其他表达式。
3) 在 xml 文件的字段定义中,您可以使用自定义 javascript/widget 进行渲染。