How can I get the current week using Python?

PythonServer Side ProgrammingProgramming

YOu can use the isocalender function from the datetime module to get the current week. Create the object of the date first, then call isocalender() on this object. This returns a 3-tuple of Year, Week number and Day of Week. 


import datetime
my_date = # if date is 01/01/2018
year, week_num, day_of_week = my_date.isocalendar()
print("Week #" + str(week_num) + " of year " + str(year))


This will give the output −

Week #1 of year 2018
Published on 08-Jan-2018 06:27:36