Python program to display various datetime formats

PythonServer Side ProgrammingProgramming

The datetime module supplies classes for manipulating dates and time. We will display different formats like day of the week, week number, day of the year, etc.

Algorithm

Step 1: Import datetime.
Step 2: Print day of the week.
Step 3: Print week number.
Step 4: Print day of the year.

Example Code

Live Demo

import datetime

print("Day of the week: ", datetime.date.today().strftime("%A"))
print("Week number: ", datetime.date.today().strftime("%W"))
print("Day of the year: ", datetime.date.today().strftime("%j"))

Output

Day of the week:  Sunday
Week number:  06
Day of the year:  045

Explanation

The arguments of the strftime() function are explained below:

  • %A: Weekday's full name (Example: 'Monday')
  • %W: Week number of the year with sunday as first day of the week
  • %j: Day of the year as a zero padded decimal number
raja
Published on 16-Mar-2021 15:38:55
Advertisements