How to Get the Current Time in Python

rajneesh

rajneesh

3 min read

  • python
get current time in python

Are you looking for a method by which you can get current time in Python?
After reading this post you can easily get current time in python. I am going to explain you 3 methods of different module by which you can get current time in python.

It's very common task while coding in python. it is mostly use While logging,scheduling, or time-stamping events.
It's is very easy due to its library's by which you can easily fetch current time.

Using the time Module

Time module provide a simple method by which you can get current time using those methods here is the code:

import time

# Get the current time in seconds since the epoch
current_time = time.time()

# Convert into human readable format
readable_time = time.ctime(current_time)

print("Current Time:", readable_time) #output:- Current Time: Wed Jun 26 15:59:57 2024

In the above code, you can see the time method by which we can get the current time but it is in seconds format, To convert the code into human readable format, we can use another method by which we convert it into human readable format that is ctime method.

Using the datetime Module

The second module is the DateTime module. It provides a more powerful and flexible way by which you can change the current time format as per your usage. here is the code:

from datetime import datetime

# this function gives you current time
now = datetime.now()

print("Current Date and Time:", now) #output:-Current Date and Time: 2024-06-26 16:01:54.691565

formatted string representation, you can use strftime:

formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Current Date and Time:", formatted_now) 
#output:-Formatted Current Date and Time: 2024-06-26 16:01:54

The DateTime module is mostly preferred by developers because of its simple format and time formatting methods.

Using pytz for Time Zones

This module help you to get current time using time zone first of all you need to install in module here is the code:

pip install pytz

Then you can you this code to get current time according to zone:

from datetime import datetime
import pytz

# Define the timezone
timezone = pytz.timezone('US/Eastern')

# current time using zone method
eastern_time = datetime.now(timezone)

print("Eastern Time:", eastern_time) #output:- Eastern Time: 2024-06-26 06:35:55.767694-04:0

Formatting Time Output

Time formatting is an important part because without formating its very hard for human understanding by the code formating to understand the time we can make it more readable and simplifed by using strftime method.

from datetime import datetime

# Current datetime
now = datetime.now()

# Different formats
date_only = now.strftime("%Y-%m-%d")
time_only = now.strftime("%H:%M:%S")
custom_format = now.strftime("%A, %B %d, %Y at %I:%M %p")

print("Date Only:", date_only) #output:- Date Only: 2024-06-26
print("Time Only:", time_only) #output:- Time Only: 10:39:56
print("Custom Format:", custom_format) #output:- Custom Format: Wednesday, June 26, 2024 at 10:39 AM

in above code i have provide some common formatting formatters you can use format according to your use case.

Conclusion

as you known get current time in python is comman problem due to which i have explain 3 methods by the different modules

like time, datetime, and pytz. according to you case you can use any module or method by which you can get current time in python. my one of the favorite one is timedate module. if you this this post is helpful for you. you can share this post with you fellow developer friends.

rajneesh

rajneesh

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.