Convert Week Number & Year to datetime in Python (3 Examples)
In this Python tutorial, you’ll learn how to convert week numbers and years to datetime objects.
The article will consist of the following information:
So now is the part you have been waiting for: the examples!
Example Data & Imported Modules
The first step is to import the datetime module and relativedelta from the dateutil module.
import datetime # loading datetime module from dateutil.relativedelta import relativedelta # loading relativedelta module
The second step is to create some sample data.
year = 2021 # setting sample year week = 4 # setting sample week
Example 1: Generating datetime via fromisocalendar()
In this section, I’ll explain how to use the fromisocalendar() function for generating a datetime object. The function needs parameters for years, week numbers, and days of the week. In this example, we will use the given data and the first day of the week as the parameter values. Note that this function is only available in Python version 3.7 or newer.
date = datetime.date.fromisocalendar(year, week, 1) # generating the datetime print(date) # printing the datetime # 2021-01-25
Example 2: Generating datetime via strptime()
This section illustrates how to utilize the strptime() function to generate a datetime object. First, the year, week number, and day of the week numerics are converted into strings for the use of strptime().
str_date = str(year) + "-" + str(week) + "-" + str(1) # preparing the string print(str_date) # printing the string # 2021-4-1
Then the string and a date-format are plugged in the strptime() function to create a datetime object.
print(datetime.datetime.strptime(str_date, "%Y-%W-%w")) # converting to datetime and printing # 2021-01-25 00:00:00
Example 3: Generating datetime via relativedelta()
The following Python code explains how to generate datetime objects via relativedelta(). First, we will generate a datetime object with the year value, which is equal to the defined data, and with the day and month values, which correspond to the first day of the first month. Then a relativedelta object is created based on the week data defined at the beginning of the tutorial and added to the initialized date to integrate the week numbers.
date = datetime.date(year, 1, 1) + relativedelta(weeks=week) # Generating the datetime object print(date) # printing the datetime # 2021-01-29
It is worth noting that this method adds 4 weeks to the first day of the year. So if it is Wednesday on 01.01 then the calculated date will also be a Wednesday. In the other examples above, the first day of the 4th week of the given year is displayed.
Video & Further Resources
In case you need further info on the topics of this article, I recommend having a look at the following video on my YouTube channel. In the video, I’m explaining the Python syntax of this tutorial:
The YouTube video will be added soon.
Besides that, you may want to read the other articles on Statistics Globe.
- Dates & Times in Python – datetime Module
- Introduction to timedelta Objects in Python
- Get Day of Week from datetime in Python – Number & Name
- Get Week Number of datetime in Python
- All Python Programming Examples
This tutorial has demonstrated how to create datetime objects from week numbers & years in the Python programming language. If you have any additional questions, don’t hesitate to let me know in the comments below.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.