How to get current date and time in Python?

In all assignments you can use:

from datetime import datetime, date, time

1) Print Current Date

Write code to print today’s date only (no time).


2) Print Current Time

Print the current time only (hours, minutes, seconds).


3) Print Full Current Date & Time

Print the current date and time in default format.


4) Print Year

Extract and print only the year from current datetime.


5) Print Month

Extract and print only the month number.


6) Print Day

Extract and print only the day of the month.


7) Print Hour

Extract and print the current hour (24-hr format).


8) Print Minute

Print the current minute from now.


9) Print Second

Print the current second.


10) Print Weekday Number

Use weekday() to print today’s weekday number (0–6).


11) Print Weekday Name

Use formatting to print today’s weekday name (e.g., “Friday”).


12) Custom Format — Date Only

Print date in format:


13) Custom Format — Time Only

Print time in format:


14) Custom Format — Date & Time

Print both with format:


15) Milliseconds Included

Print current date & time including microseconds.


16) AM/PM Format

Print current time in 12-hour format with AM/PM.


17) Print Today’s ISO Format

Use .isoformat() to print the current date/time.


18) Separate Date & Time Variables

Get current datetime once, then print:


19) Print Time Without Microseconds

Print current time but without microseconds (use string formatting or .replace()).


20) Print Timezone-aware Now

(Optional if covered) Print the current time with timezone info (using datetime.now(tz=…)).


🧠 Concepts Practiced

Concept
Code

Current datetime

datetime.now()

Current date only

date.today() or now.date()

Extract year/month/day

now.year, now.month, now.day

Extract time parts

now.hour, now.minute, now.second

Weekday

now.weekday() or formatted string

Formatting

strftime() or .isoformat()


📌 Starter Code Snippets

Print current date

Print current date & time

Formatted output

12-hour time


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated