Skip to content

Instantly share code, notes, and snippets.

@sehrishnaz
Created July 29, 2021 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sehrishnaz/7b5325bd886281793aac3a14e2c200be to your computer and use it in GitHub Desktop.
Save sehrishnaz/7b5325bd886281793aac3a14e2c200be to your computer and use it in GitHub Desktop.
Produce a date for every month from start until end in Odoo
from datetime import datetime, timedelta, date
class some_model(models.Model):
_name = 'some.model'
# creating date field in odoo class
date_start = fields.Date(string="Start Date")
date_end = fields.Date(string="End Date")
@api.multi
def calc_first_date_of_month(self,from_date, end_date, day=1):
"""Produce a date for every month from start until end"""
start = from_date.year * 12 + (from_date.month - 1)
end = end_date.year * 12 + (end_date.month - 1)
if end_date.day > day:
# end date is past the reference day, include the reference
# date in the output
end += 1
# generate the months, just a range from start to end
for ordinal in range(start, end):
yield date(ordinal // 12, (ordinal % 12) + 1, day)
@api.one
def button_click(self):
# converting string date value into python date object
date_start = datetime.strptime(self.date_start, '%Y-%m-%d').date()
date_end = datetime.strptime(self.date_end, '%Y-%m-%d').date()
list_of_date = list(self.calc_first_date_of_month(date_start, date_end))
print('***',list_of_date)
@sehrishnaz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment