Home > Net >  Python print something on a specific day
Python print something on a specific day

Time:07-27

I want to create a python code that can print messages on specific days for example something like that:

if day == 27.07:

   print("Happy Birthday") 

I thank you for any help

CodePudding user response:

Here's a way:

from datetime import datetime
birthday = '26.07'
if datetime.today().strftime('%d.%m') == birthday:
    print("Happy Birthday")

Docs:

CodePudding user response:

Hope this helps!

from datetime import date

today = date.today()
if today == "2022-07-26":
  print("Happy Birthday")

CodePudding user response:

We can do this for a particular day as mentioned above or a range of days if required.

1. For only One day

Taking a specific date for example "26/07/1999" and lets print happy birthday if today coincides with our specific day.

from datetime import datetime

birthday = "26/07/1999"

BRDAY = datetime.strptime(birthday, '%d/%m/%Y')

Today = datetime.now()

if(BRDAY.day == Today.day and BRDAY.month == Today.month):
    print("Happy Birthday ")

2. For Multiple days

Say, we have a list of days "1/07/1922","15/07/1922","22/07/1922" and "26/07/1922" and we have to print something on those days then we can try:

from datetime import datetime

Days = ["1/07/1922","15/07/1922","22/07/1922","26/07/1922"]
BRDAY=[]

for i in range(len(Days)):
    var=Days[i]
    BRDAY.append(datetime.strptime(var, '%d/%m/%Y'))

Today = datetime.now()



for i in range(len(Days)):
    if(BRDAY[i].day == Today.day and BRDAY[i].month == Today.month):
        print("Happy Birthday ")

3. To find if there is a specific day in a list of days

Say we have to search for "15/07/1922" in the list of ["1/07/1922","15/07/1922","22/07/1922","26/07/1922"] and if found, python will print Found, we can try the following:

SPECIFIC="15/07/1922"
SPECIFIC=datetime.strptime(birthday, '%d/%m/%Y')
        
for i in range(len(Days)):
    if(SPECIFIC.day == BRDAY[i].day and BRDAY[i].month == SPECIFIC.month):
        print("Found")

Run the full program step by step, you'll get it. Happy Testing!

  • Related