Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to count days and weeks and months in selected period of time

By
Dr Obx
on 12/21/15, 5:10 AM 443 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


.....

2

Ankit H Gandhi(AHG)

--Ankit H Gandhi(AHG)--
996
| 5 3 6
Rajkot, India
--Ankit H Gandhi(AHG)--

Learning Python..

Ankit H Gandhi(AHG)
On 12/21/15, 7:34 AM

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

Thank you Ankit. Checking ;)

Dr Obx
on 12/21/15, 8:02 AM

Your Welcome Dr Obx !!!

Ankit H Gandhi(AHG)
on 12/21/15, 8:09 AM

Works beautifully :)

Dr Obx
on 12/21/15, 8:58 AM

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

Dr Obx
on 12/21/15, 4:18 PM

You can get months from start date or end date

Ankit H Gandhi(AHG)
on 12/22/15, 5:53 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/21/15, 5:10 AM
Seen: 443 times
Last updated: 12/21/15, 4:50 PM