howtouselinux

3 Ways to Get Current Time in Python

Table of Contents

Python has a few different ways to get the current time. In this blog post, we will explore three of them. Each method has its own benefits and drawbacks, so it’s important to understand them all before deciding which one to use. Let’s get started!

3 ways to get current time in Python:

  • use now function in Python datetime module: import datetime ; print(datetime.datetime.now())
  • use localtime function in Python time module: import time; time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime())
  • use system function in Python os module: import os; os.system(‘date +”%Y-%m-%d %H:%M:%S”‘)

 

Get Current Time with datetime module in Python

The easiest way to get the current time in Python is using datetime.now() method. It is a built-in function.This method returns a datetime object with the current date and time. Here is one code example:

  • import datetime
  • print(datetime.datetime.now())

 

>>> from datetime import datetime
>>> now = datetime.now()
>>> print(“The current date is:”, now.strftime(“%Y-%m-%d %H:%M:%S”))
The current date is: 2022-02-20 10:19:14

>>> currentDT = datetime.now()
>>> print (“Current Year is: %d” % currentDT.year)
>>> print (“Current Month is: %d” % currentDT.month)
>>> print (“Current Day is: %d” % currentDT.day)
>>> print (“Current Hour is: %d” % currentDT.hour)
>>> print (“Current Minute is: %d” % currentDT.minute)
>>> print (“Current Second is: %d” % currentDT.second)
>>> print (“Current Microsecond is: %d” % currentDT.microsecond)

Here are four examples.
import datetime
currentDateTime = datetime.datetime.now()
print(currentDateTime)
———————————————————
from datetime import datetime
currentDateTime = datetime.now()
print(currentDateTime)
———————————————————
import datetime
print(datetime.datetime.now())
———————————————————
from datetime import datetime
print(datetime.now())

Get Current Time with time module in Python

The second way to get the current time is with the localtime() function from the time module. time.localtime() method of Time module is used to convert a time expressed in seconds since the epoch to a time.struct_time object in local time. This will return the current time of your Locale. This also returns the complete date and time information which can be formatted using the strftime() to display only the time as shown below.

>>> import time
>>> t = time.localtime()
>>> current_time = time.strftime(“%Y-%m-%d %H:%M:%S”, t)
>>> print(current_time)
2022-02-20 10:19:14

Get Current Time with os module in Python

The third way to get the current time is with the system function from the os module. The os module in Python is a powerful tool that allows you to do many things, including manipulating files and directories, reading and modifying environment variables, and checking the status of your system.

>>> import os
>>> os.system(‘date’)
Sun Feb 20 10:12:36 UTC 2022
>>> os.system(‘date +”%Y-%m-%d %H:%M:%S”‘)
2022-02-20 10:30:09