Question: What would a piece of code look like which checks today's date, and if it's a particular date, it will print out a message?

The context is that I'm making a birthday cake for my son who codes and I want to write a themed Happy Birthday message on top correctly. (Sorry, not as clever or serious as a lot of the things on here!)

I'd like something which is basically:

```
johnsBirthday = 01/01/1998
johnsAge = todaysdate - johnsBirthday (in years)
if todays date == 01/01/XXXX then print("Happy Birthday John!" + johnsAge + " today!")

```

My knowledge of python is very limited (as I'm sure you can tell from the above) but I vaguely know how to do this in Excel, so I figure there must be a way to do it in python too?

I know could always just write out:

```
print("Happy Birthday, John!")

```

and he'd appreciate that, but I think it would really make him smile to go a little further than that!

Answer: 
```
# Import datetime class from datetime module
from datetime import datetime

birthday = "20/09/1998"

# Parses the string into a datetime object
birthday_obj = datetime.strptime(birthday, '%d/%m/%Y')

# Gets todays date
now = datetime.now()

# Checks the day and month to verify birthday status
if(birthday_obj.day == now.day and birthday_obj.month == now.month):
    johns_age = str(now.year - birthday_obj.year)
    print("Happy Birthday John! " + johns_age + " today!")

```
For your purpose, it might be easier to use regular expressions if you are familiar with them. You can search any patterns you like, after converting datetimes to string, or better yet if you already have datetimes in string formats.

For datetime to string format conversion codes, check out -
[format-codes](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes)

Example code

```
import re
from datetime import datetime

pattern = re.compile(r'^01/01/')
today_date = datetime.now().strftime(r'%d/%m/%Y')
some_date = '01/01/2021'

print(re.match(pattern, some_date)) # <re.Match object; span=(0, 6), match='01/01/'>

print(today_date)                     # 20/09/2020
print(pattern.match(today_date))      # None

```

Edit - Had forgotten that age needs to be calculated!

```
import re
from datetime import datetime

johns_birthday = '01/01/1998'
if (re.match('^01/01/', johns_birthday)):
  johns_age = datetime.now().year - datetime.strptime(johns_birthday, r'%d/%m/%Y').year
  print("Happy Birthday John! " + str(johns_age) + " today!")

```