如何在odoo中创建预期的日期时间?

2024-10-01 09:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建从工厂日起2个月的预期日期时间。我的代码是:

@api.one
@api.depends('date_plant','nursery_plandate')
def calculateplandate(self):
    fmt = '%Y-%m-%d'
    if self.date_plant:
        d1 = self.date_plant
        conv = datetime.date(d1)
        d2 = datetime.strptimes(str(conv),fmt)
        d3 = d2.month
        hasil = d3+2
        self.nursery_plandate = hasil

我的错误是:

^{pr2}$

Tags: selfapidatetimedate工厂时间d2d1
3条回答

在odoo 9.0中-字段。日期以及字段。日期时间有from_string()和to_string()方法。 您可以使用这些来创建日期/日期时间对象,然后根据需要进行操作。在

date = fields.Date.from_string(self.date_plant)

此错误的原因是,您将字符串而不是日期传递给日期时间.日期()。当我们收到一个日期数据时,它将是字符串类型,你必须把它转换成日期类型。在

试试这个代码:-I我包括一个样本代码增加60天你的工厂日期。请根据您的需要进行必要的修改。在

def calculateplandate(self):
    if self.date_plant:
        start = datetime.strptime(self.date_plant, DEFAULT_SERVER_DATE_FORMAT)
        conv = datetime.date(d1)
        hasil = start + datetime.timedelta(days=60) # for adding 60 days
        self.nursery_plandate = hasil

希望这有帮助。在

谢谢你的回答,我为我的案子找到了另一个答案,像这样:

 def calculateplandate(self):
    fmt = '%Y-%m-%d'
    if self.date_plant:
        from_date = self.date_plant
        d1=datetime.strptime(str(from_date),fmt)
        date_after_month = datetime.date(d1)+ relativedelta(months=1)
        cetak = date_after_month.strftime(fmt)
        self.nursery_plandate = cetak

我发现了另一个问题,我不能得到月的价值浮动,例如我得到价值月为1,5,我希望从种植日的预期日是1,5个月。在

相关问题 更多 >