How to find the first date of a given year using Python?

The datetime module in Python can be used to find the first day of a given year. This datetime module is widely used for manipulating dates and times in various formats and calculations.

Common approaches to find the first day of a given year using Python are as follows ?

  • Datetime Module − Widely used library for manipulating dates and times in various ways.

  • Calendar Module − Provides functions related to calendar and date manipulation.

  • Time Module − Works with time-related tasks, particularly focusing on time intervals and formatting.

Using datetime Module

By specifying January 1st of any year, we can easily find the day of the week for that date. Creating a date object for January 1st of the given year can be done using datetime.date() function.

Example

In the below code strftime("%d %b %Y %A") formats the date to display day, month, year, and weekday name ?

import datetime

def first_day_of_year(year):
    # Create a date object for January 1st of the given year
    first_day = datetime.date(year, 1, 1)
    # Format the date to display the day, month, year, and weekday
    return first_day.strftime("%d %b %Y %A")

# Test with different years
print(first_day_of_year(2024))
print(first_day_of_year(2025))
print(first_day_of_year(2020))

The output of the above code is ?

01 Jan 2024 Monday
01 Jan 2025 Wednesday
01 Jan 2020 Wednesday

Using calendar Module

The calendar module provides functions to work with dates and can be used to find the day of the week for any given date.

Example

In the below example, calendar.weekday(year, month, day) returns the day of the week as an integer (0=Monday, 6=Sunday), and calendar.day_name[day_of_week] converts the integer to the full name of the day ?

import calendar

def first_day_of_year(year):
    month = 1
    day = 1
    
    # Get the day of the week as integer
    day_of_week = calendar.weekday(year, month, day)
    
    # Convert the day index to a weekday name
    weekday_name = calendar.day_name[day_of_week]
    
    # Format the date and return it
    return f"{day:02d} Jan {year} {weekday_name}"

# Test with different years
print(first_day_of_year(2024))
print(first_day_of_year(2020))
print(first_day_of_year(2023))

The output of the above code is ?

01 Jan 2024 Monday
01 Jan 2020 Wednesday
01 Jan 2023 Sunday

Using time Module

The time module can also handle date operations. We can create a time tuple for January 1st and format it using strftime().

Example

In the below code, we create a time tuple for the first day of the year and format it using strftime() ?

import time

def first_day_of_year(year):
    # Create a time tuple for January 1st of the given year
    # (year, month, day, hour, minute, second, weekday, yearday, dst)
    first_day_tuple = (year, 1, 1, 0, 0, 0, 0, 0, 0)
    
    # Format the time tuple to a readable string
    formatted_date = time.strftime("%d %b %Y %A", first_day_tuple)
    return formatted_date

# Test with different years
print(first_day_of_year(2024))
print(first_day_of_year(2023))
print(first_day_of_year(2025))

The output of the above code is ?

01 Jan 2024 Monday
01 Jan 2023 Sunday
01 Jan 2025 Wednesday

Comparison

Method Module Best For
datetime.date() datetime Simple date operations
calendar.weekday() calendar Calendar-specific operations
time.strftime() time Time formatting and manipulation

Conclusion

The datetime module provides the most straightforward approach for finding the first day of a year. Use calendar module when you need additional calendar functions, and time module for more complex time operations.

Updated on: 2026-03-25T17:52:49+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements