get the date for the first day of the current century in python

You can get the date for the first day of the current century in Python by using the datetime module and performing some simple calculations. Here's an example code snippet:

main.py
import datetime

# get current year
current_year = datetime.datetime.now().year

# get start year of current century
start_year = (current_year // 100) * 100

# create date object for first day of current century
first_day = datetime.date(start_year, 1, 1)

print(first_day)
275 chars
13 lines

This code first gets the current year using the datetime module. It then calculates the start year of the current century by dividing the current year by 100, rounding down, and then multiplying by 100. For example, if the current year is 2021, the start year of the current century is 2000.

Finally, the code creates a date object for the first day of the current century by passing in the start year, 1 for January, and 1 for the first day of the month. The output will be in the format "YYYY-MM-DD".

gistlibby LogSnag