def get_first_date_for_group(start_date,group_type,n):
    """
    :param start:   start date
    :n          :   how many groups we want to get
    :group_type :   daily, weekly, monthly
    """
    current_date = start_date
    if group_type == 'monthly':
        current_year = start_date.year
        current_month = start_date.month
        for i in range(n-1):
            current_month-=1
            if current_month == 0:
                current_month = 12
                current_year -= 1
        first_date = datetime.datetime(current_year,current_month,1)
    elif group_type == 'weekly':
        first_date=start_date-datetime.timedelta(days = start_date.weekday()+(n-1)*7)
    elif group_type == 'daily':
        first_date = start_date-datetime.timedelta(days = n-1)
    first_date = datetime.datetime(first_date.year,first_date.month,first_date.day,0,0,0)
    return first_date
def get_first_date_for_group ( start_date , group_type , n ) : current_date = start_date if group_type == 'monthly' : current_year = start_date . year current_month = start_date . month for i in range ( n - 1 ) : current_month -= 1 if current_month == 0 : current_month = 12 current_year -= 1 first_date = datetime . datetime ( current_year , current_month , 1 ) elif group_type == 'weekly' : first_date = start_date - datetime . timedelta ( days = start_date . weekday ( ) + ( n - 1 ) * 7 ) elif group_type == 'daily' : first_date = start_date - datetime . timedelta ( days = n - 1 ) first_date = datetime . datetime ( first_date . year , first_date . month , first_date . day , 0 , 0 , 0 ) return first_date
:param start:   start date
    :n          :   how many groups we want to get
    :group_type :   daily, weekly, monthly