Created
July 29, 2021 06:32
-
-
Save sehrishnaz/7b5325bd886281793aac3a14e2c200be to your computer and use it in GitHub Desktop.
Produce a date for every month from start until end in Odoo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Detail and Code Description: https://learnopenerp.blogspot.com/2021/07/how-to-use-and-convert-date-field-format-odoo.html