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. 

Example

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

Output

This will give the output −

Week #1 of year 2018
raja
Published on 08-Jan-2018 10:57:36
Advertisements