Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Find the First Date of a Given Year Using Python



The datetime module in Python can be used to find the first day of the given year. In general, this datetime module is mostly used for manipulating dates and times. Some of the Common approaches to print the first day of the given year by 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: To Work with time-related tasks, particularly focusing on time intervals and formatting.

Using datetime module

By specifying the first day of the year (January 1st), so we can easily find the day of the week. 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") represents the format of the required date.

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")

print(first_day_of_year(2024))

Following is the output of the above code ?

01 Jan 2024: Monday

Using the calendar Module

The calendar module provides functions to work with dates. So we can use it to find the day of the week for the required date.

Example

In the below example code, the calendar.weekday(year, month, day) function will return the day of the week as an integer, and the calendar.day_name[day_of_week] function converts the integer to full name of the day.

import calendar

def first_day_of_year(year):
    month = 1
    day = 1
    
    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}"

print(first_day_of_year(2020))

Following is the output of the above code ?

01 Jan 2020: Wednesday

Using the time Module

Another way to handle dates is by using the time module. Mostly used for measuring performance, and handling time zones, etc.

The time module provides strftime() function which converts a tuple or struct_time representing, either in UTC or the local time, to a 24-character string of the following form: 'Mon Jan 9 10:51:77 2023'.

The strptime() method parses a string representing a time, either in UTC or local time, according to a format. The method accepts two arguments: one being the time to be parsed as a string, and the other argument is the format specified. The return value will be obtained as an object struct_time.

Example

In the below code strftime() will format a 'struct_time' as a string, and the strptime() function will parse a string into a 'struct_time'.

import time

current_time = time.time()

# Format the current time
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(current_time))

# Pause execution for 2 seconds
time.sleep(2)

print(formatted_time)

Following is the output of the above code ?

2024-08-28 11:48:15
Updated on: 2025-02-27T17:04:07+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements