Last active
July 22, 2018 01:32
-
-
Save vubon/e22039900b85adea961966d3feef3e9f to your computer and use it in GitHub Desktop.
Finding date in two dates range by Python
This file contains hidden or 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 date, timedelta, datetime | |
start_date = date(2018,1,1) | |
end_date = datetime.now() | |
every_month = 28 | |
def finding_date(start_date, end_date, every_month): | |
start_month = start_date.month | |
end_months = (end_date.year-start_date.year)*12 + end_date.month | |
has_month = [] | |
has_not = 0 | |
for month in range(start_month, end_months + 1): | |
year = (month-1)//12 + start_date.year | |
month = (month-1) % 12 + 1 | |
# last_date_of_month = date(year, month, 1) - timedelta(days=1) | |
try: | |
has_month.append(date(year, month, every_month)) | |
except ValueError as e: | |
has_not += 1 | |
#print(e) | |
return len(has_month), has_not | |
correct, incorrect = finding_date(start_date, end_date, every_month) | |
if incorrect == 0: | |
print('Every month date is correct {}'.format(every_month)) | |
else: | |
print('Your start date {0} to end date {1} has only {2} months {3} date '.format(start_date, end_date, correct, every_month)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment