davdis
davdis

Reputation: 253

Print specific date in python

I want to create a function that will return a date depending on each month. More specific I want it to return to me the 23d day of each month in the example format (YYYY, MM, DD):
2018, 1, 23 .

except for the occasion that this date coincides with a weekend, in which I want it to return the exact previous working day. For example for December 2017 the 23d was Saturday, so in this case I want my function to return to me 2017, 12, 22.

Right now I use:

import datetime
someday = datetime.date(2018, 1, 23)
    print(someday)

with which in the someday variable I define the desired date manually for each month.

In short for the next seven months of 2018 (January, February, March, April, May, June, July), I want my function to return to me :

2018, 1, 23
2018, 2, 23
2018, 3, 23
2018, 4, 23
2018, 5, 23
2018, 6, 22
2018, 7, 23

and so on and so forth for the next months.

Upvotes: 2

Views: 2284

Answers (2)

Fusseldieb
Fusseldieb

Reputation: 1374

I wrote a small python script on how you could do it.

Use toordinal to check for the day of the week and if it's Saturday or Sunday, subtract 1 or 2 by using timedelta

import datetime

# Yes, from 1,13 because 13 is exclusive (Goes only to 12)
days_month = datetime.monthrange(2018,x)
for x in range(days_month[0],days_month[1]+1):
    for x in range(1, monthrange):
        someday = datetime.date(2018, x, 23) # Loop over every month
        weekday = someday.weekday() # 0 = Monday, 6 = Sunday
        if(weekday > 5 ): # If it's "more" than Friday
            jumpback_days = weekday - 4; # Subtract 4 from the current weekday to get 1 for Saturday and 2 for Sunday
            print(someday - datetime.timedelta(days=jumpback_days)) # Subtract days and print
        else:
            # Print without subtracting anything
            print(someday)

Note: If you are using Python2, please replace range with xrange for it to work.

EDIT: If you want to print only and all workdays in a specific year, you can do it this way:

import datetime
from calendar import monthrange

year = 2018
for month in range(1, 13): # Yes, from 1,13 because 13 is exclusive (Goes only to 12)
    days_month = monthrange(year, month) # Request the amount of days in that month
    for day in range(1, days_month[1] + 1): # Iterate through all days of that month (+1 because it's exclusive)
        someday = datetime.date(year, month, day) # Loop over every month
        weekday = someday.weekday() # 0 = Monday, 6 = Sunday
        if(weekday < 5 ): # If it's "less" than Saturday
            # Print because if it's "less" than Saturday, then it's a workday
            print(someday)

Upvotes: 1

zipa
zipa

Reputation: 27869

You can achieve this with weekday(), timedelta() and while loop:

from datetime import date, timedelta

def last_workday(somedate):
    while somedate.weekday() > 4: #weekday returns values 0-6 (Mon-Sun)
        somedate -= timedelta(days=1) #timedelta returns 1 day which you subtract from your date 
    return somedate

y = 2018
d = 23

for m in range(1,13):
    print(last_workday(date(y, m, d)))

Upvotes: 1

Related Questions