Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return two dates in a function

I have the code below:

import datetime

def get_date(x, y):
    last_day = date.today().replace(day=1) - timedelta(days=1)
    start_day= date.today().replace(day=1) - timedelta(days=last_day_of_prev_month.day)
    start_day_of_prev_month = start_day.strftime('%Y-%m-%d')
    last_day_of_prev_month= last_day.strftime('%Y-%m-%d')

    today = date.today().strftime('%Y-%m-%d')
    firstDayOfMonth = date.today().replace(day=1).strftime('%Y-%m-%d')
    lastDay = sf.date_sub(sf.current_date(), 1)
    if today == firstDayOfMonth:
        x = start_day_of_prev_month,
        y = last_day_of_prev_month
    else:
        x = firstDayOfMonth,
        y = lastDay
    return 0

I need it to return two dates according to the "if" above ‘x’,’y’.
The output would be for example:

'2022-08-01','2022-08-18' 

But, this function don’t return this output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can anyone help me?

>Solution :

This implements the following logic: If today is the first day of the month, return (first day of previous month, last day of previous month), otherwise return (first day of current month, yesterday):

def get_data():
    today = date.today()
    if today.day == 1:
        x = today.replace(month=today.month-1, day=1)
        y = today.replace(day=1) - timedelta(days=1)
    else:
        x = today.replace(day=1)
        y = today - timedelta(days=1)
    return x, y

or even simpler, as this is equivalent to (first day of yesterday’s month, yesterday):

def get_data():
    today = date.today()
    y = today - timedelta(days=1)
    x = y.replace(day=1)
    return x, y
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading