Google News
logo
Python Program to Return the year and name of weekday
In the following example of Python program that imports the `datetime` module, retrieves the current date and time, and returns the year and name of the weekday:
Program :
import datetime

# Get the current date and time
now = datetime.datetime.now()

# Extract the year from the current date
year = now.year

# Get the name of the weekday
weekday = now.strftime("%A")

# Print the year and weekday name
print("Year: ", year)
print("Weekday: ", weekday)
Output :
Year:  2023
Weekday:  Thursday
The `strftime()` method is used to format the date and time in the desired string format.

In this case, the `%A` directive is used to retrieve the full name of the weekday.