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

时间:2022-08-25 18:46:57

How to import a date from view and calculate the number of days remaining for another date?

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

Here is the solution I tried but it's not working:

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

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 !"),
        ]

1 个解决方案

#1


3  

you have to do it properly:

你必须正确地做到:

  • specify correct date/time format

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

  • parse datetime from string

    从字符串解析datetime

  • substract the same data types: datetime - datetime

    减去相同的数据类型:datetime - datetime

Code:

码:

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

#1


3  

you have to do it properly:

你必须正确地做到:

  • specify correct date/time format

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

  • parse datetime from string

    从字符串解析datetime

  • substract the same data types: datetime - datetime

    减去相同的数据类型:datetime - datetime

Code:

码:

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