Thursday, 2 November 2017

Odoo , Python : To get month name list based on dates duration.

import calendar

# print calendar.month_name[9] # give your month number and it will show exact month name.


import datetime
import time
from datetime import date
from dateutil.rrule import rrule, MONTHLY
# give your date duration
date_from = '01/03/2016'
date_to = '30/04/2017'

date_from_date = date_from[:2]
date_from_month = date_from[3:5]
date_from_year = date_from[6:]

date_to_date = date_to[:2]
date_to_month = date_to[3:5]
date_to_year = date_to[6:]
#
date1 = date(int(date_from_year), int(date_from_month), int(date_from_date))
date2 = date(int(date_to_year), int(date_to_month), int(date_to_date))

months = [dt.strftime("%m") for dt in rrule(MONTHLY, dtstart=date1, until=date2)]
print months

for mo in [dt.strftime("%m") for dt in rrule(MONTHLY, dtstart=date1, until=date2)]:
    print calendar.month_name[int(mo)]