gpt4 book ai didi

python - 如何计算另一个日期的剩余天数

转载 作者:行者123 更新时间:2023-11-30 22:57:09 24 4
gpt4 key购买 nike

如何从 View 中导入日期并计算另一个日期的剩余天数?

这是我尝试过的解决方案,但它不起作用:

class saisir_soumi(osv.osv):
_name='saisir.soumi'

def compa_date(self,cr,uid,ids,args,fields,context=None):
r = {}
date_format = "%D/%M/%Y"
joining_date = 'date_depot'
current_date = '29/04/2016 02:02:02'# current date
d1 = datetime.strptime(joining_date, date_format)
d2 = datetime.strptime(current_date, date_format)
diff = current_date - datetime.date.today()
return diff.days

_rec_name = 'NumOffre'

_columns = {
'NumOffre' : fields.char('N° Offre',required=True),
'organisme_s' : fields.char('Organisme',required=True),
'date_depot' : fields.datetime('Date dépot de soumission'), # the date to be seized and used for its difference with current date( today)
'jrestant': fields.function(compa_date,string='Jours restant')
}
_sql_constraints = [
('uniq_NumOffre', 'unique(NumOffre,id)', "numero offre doit resté unique !"),
]

最佳答案

你必须正确地做到这一点:

  • 指定正确的日期/时间格式

  • 从字符串中解析日期时间

  • 减去相同的数据类型:日期时间 - 日期时间

代码:

In [68]: current_date = '29/04/2016 02:02:02'

In [69]: date_format = '%d/%m/%Y %H:%M:%S'

In [70]: (datetime.datetime.strptime(current_date, date_format) - datetime.datetime.now()).days
Out[70]: 5

关于python - 如何计算另一个日期的剩余天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com