Python Get Current Time

Most of these will use:

from datetime import datetime, time
now = datetime.now()

1) Print Only Current Time

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


2) Print Hours Only

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


3) Print Minutes Only

Extract and print the current minute.


4) Print Seconds Only

Extract and print the current second.


5) Print Microseconds

Extract and print the current microsecond.


6) 12-Hour Format

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

Example output:


7) Format HH:MM

Print current time only with:


8) Format HH:MM:SS

Print current time in:


9) Time With Milliseconds

Print time including microseconds.


10) Split Date & Time

Store current datetime in now. Print now.date() and now.time() separately.


11) Print Hour > 12?

Ask user for current hour and print whether it’s before noon or after noon.


12) Check AM or PM

Use now.strftime() to determine and print AM or PM.


13) Display Time in Words

Print time in this format:


14) Sleep and Show Updated Time

Import time module, pause the program for 3 seconds with time.sleep(3), then print updated time.


15) Time Comparison

Get current time and compare it with a fixed time:

Print whether now is before or after 2 PM.


16) Time in UTC

Print current UTC time using datetime.utcnow().


17) Print Local vs UTC

Print both local current time and UTC time.


18) Time from User Input

Ask user to enter a time string in:

Parse it using datetime.strptime() and print the parsed time.


19) Time Without Microseconds

Print the current time but do not show microseconds.


20) Time Components as Variables

Assign:

Then print:


🧠 Concepts Practiced

Topic
Typical Method

Get current time

datetime.now()

Extract hour/minute/second

.hour, .minute, .second

12-hour format

strftime("%I:%M %p")

UTC time

datetime.utcnow()

Pause execution

time.sleep(sec)

Parsing time

datetime.strptime()


📌 Quick Code Snippets

Print current time

Extract parts

12-hour time with AM/PM

Without microseconds

Sleep and update


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

Last updated