0

I have the following issue with python with script in linux: I am executing my_query.py passing as a parameter the date 2017-03-01

python my_query.py 2017-03-01

How can I create a loop to iterate over that date so it cover the entire month (from date 1 to end of month).

Thanks!

2
  • 2
    python datetime docs. Commented Jun 7, 2017 at 13:40
  • 3
    Which part are you specifically having trouble with? Getting the command line arg? Converting it to a datetime object? Creating an iterator? Can you show what you've tried so far? Commented Jun 7, 2017 at 13:42

2 Answers 2

2

You can use monthrange from calendar

import sys
from calendar import monthrane

datestr = sys.argv[1]

year, month, day = datestr.split('-')

for day in xrange(1, monthrange(int(year), int(month))[1] + 1):

    print day
Sign up to request clarification or add additional context in comments.

Comments

1
from dateutil.relativedelta import relativedelta
from dateutil.parser import parse
from datetime import timedelta


def dates_between(start, end):
    while start <= end:
        yield start
        start += timedelta(days=1)


start = parse('2017-03-01').date()

for day in dates_between(start, start + relativedelta(months=1)):
    print(day)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.