get current week in python

To get the current week in Python, you can use the datetime module. Here's an example:

main.py
import datetime

# Get the current date
today = datetime.date.today()

# Calculate the current week number
week_number = today.isocalendar()[1]

# Print the current week number
print("Current week:", week_number)
213 chars
11 lines

This code uses the isocalendar() function from the date object, which returns a tuple containing the year, week number, and weekday. We retrieve the week number from the tuple and print it.

Make sure to import the datetime module at the beginning of your code in order to use its functions and objects.

related categories

gistlibby LogSnag