Skip to Content
Menu
This question has been flagged
1 Reply
7118 Views

Hello everyone,

Got this code done but it wont work. What should I do to get number of days and weeks ?

Two fields: days and weeks defined as a _computed fields

.....

 _columns = {

'st_date': fields.date('Start date'),

'en_date': fields.date('End date'),

'product': fields.one2many('product.data','check_id', string="Product"),

'days': fields.char(_compute='days_between', string='NoD'),

'weeks': fields.char(_compute='weeks_between', string='NoW'),

}

def days_between(st_date,en_date):

st_date = datetime.strptime('st_date', "%Y-%m-%d")

en_date = datetime.strptime('en_date', "%Y-%m-%d")

return abs((en_date - st_date).days)

print 'test==days'

print days_between('2013-05-06', '2013-06-06')


def weeks_between(st_date,en_date):

weeks = rrule.rrule(rrule.WEEKLY, dtstart=st_date, until=en_date)

return weeks.count()

print 'test==weeks'

print 'weeks between', weeks


.....

Avatar
Discard
Best Answer

Dear Dr Obx,

Use below code may be help for you.

from datetime import datetime, timedelta

def _days_between(self, cr, uid, ids, field_name, arg, context=None):

    res = {}

    for cur_data in self.browse(cr, uid, ids, context=context):

        st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")

        en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")

        res[cur_data.id] = abs((en_date - st_date).days)

    return res

def _weeks_between(self, cr, uid, ids, field_name, arg, context=None):

    res = {}

    for cur_data in self.browse(cr, uid, ids, context=context):

        st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")

        en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")

        week_day1 = (st_date - timedelta(days=st_date.weekday()))

        week_day2 = (en_date - timedelta(days=en_date.weekday()))

        res[cur_data.id] = (week_day2 - week_day1).days / 7

    return res

_columns = {

'st_date': fields.date('Start date'),

'en_date': fields.date('End date'),

'days': fields.function(_days_between, type='char', string='NoD'),

'weeks': fields.function(_weeks_between, type='char', string='NoW'),

}

Best Regards,

Ankit H Gandhi

Avatar
Discard
Author

Thank you Ankit. Checking ;)

Your Welcome Dr Obx !!!

Author

Works beautifully :)

Author

By the way, Ankhit what about months ? will it work same way if I change some details in "weeks ?"

You can get months from start date or end date

Related Posts Replies Views Activity
0
Nov 19
1903
5
Mar 16
8809
3
Sep 24
155
0
Dec 23
374
0
Oct 21
1796