Monday, 3 October 2016

Odoo Python Date Range : Find out the date range as list using two date.


Step 1 : Create a file as validations.py and put the below code
-------------------
def get_date_range(date_from, date_to):
    date_from_year = date_from[:4]
    date_from_month = date_from[5:7]
    date_from_date = date_from[8:11]
   
    date_to_year = date_to[:4]
    date_to_month = date_to[5:7]
    date_to_date = date_to[8:11]
   
    d1 = date(int(date_from_year), int(date_from_month), int(date_from_date))
    d2 = date(int(date_to_year), int(date_to_month), int(date_to_date))
    delta = d2 - d1
    date_range_list = []
    for i in range(delta.days + 1):
        r_date = d1 + td(days=i)
        date_range_list.append(str(r_date))
    return date_range_list
--------------------

Step 2 : Use the following code in wherever you want to find out the date range and to return in list format.
--------------------
date_list = validations.get_date_range(date_from, date_to)
--------------------

FUI:
you can separate the date as year, month, date using below code too.
date_from_year = date_from[:4]
    date_from_month = date_from[5:7]
    date_from_date = date_from[8:11]

No comments:

Post a Comment