Get Current Date and Time using Python

22 Jun, 2022
In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python.
Current time using DateTime objectGet time using the time module
Current time using DateTime object
Get time using the time module
In this example, we will learn How to get the current Date and Time using Python. In Python, date and time are not data types of their own, but a module named DateTime can be imported to work with the date as well as time. Datetime module comes built into Python, so there is no need to install it externally. To get both current date and time datetime.now() function of DateTime module is used. This function returns the current local date and time.
In this example, we will use the DateTime object for getting the date and time using datetime.now().
Python3
# Getting current date and time using now(). # importing datetime module for now()import datetime # using now() to get current timecurrent_time = datetime.datetime.now() # Printing value of now.print("Time now at greenwich meridian is:", current_time)
Output:
Time now at greenwich meridian is: 2022-06-20 16:06:13.176788
timedate.now() have different attributes, same as attributes of time such as year, month, date, hour, minute, and second.
Python3
# Python3 code to demonstrate# attributes of now() # importing datetime module for now()import datetime # using now() to get current timecurrent_time = datetime.datetime.now() # Printing attributes of now().print("The attributes of now() are :") print("Year :", current_time.year) print("Month : ", current_time.month) print("Day : ", current_time.day) print("Hour : ", current_time.hour) print("Minute : ", current_time.minute) print("Second :", current_time.second) print("Microsecond :", current_time.microsecond)
Output:
The attributes of now() are :
Year : 2022
Month :  6
Day :  20
Hour :  16
Minute :  3
Second : 25
Microsecond : 547727
In this example, it can be seen that the above code does not give the current date and time of your timezone. To get the date and time for a particular timezone now() takes timezone as input to give timezone-oriented output time. But these time zones are defined in pytz library. 
Python3
# for now()import datetime # for timezone()import pytz # using now() to get current timecurrent_time = datetime.datetime.now(pytz.timezone('Asia/Kolkata')) # printing current time in indiaprint("The current time in india is :", current_time)
Output:
The current time in india is : 
2019-12-11 19:28:23.973616+05:30
UTC Stands for Coordinated Universal Time, These times are useful when you are dealing with Applications that have a global user for logging the events. You can get the current time in UTC by using the datetime.utcnow() method
Python3
from datetime import datetime print("UTC Time: ", datetime.utcnow())
Output:
UTC Time:  2022-06-20 11:10:18.289111
isoformat() method is used to get the current date and time in the following format: It starts with the year, followed by the month, the day, the hour, the minutes, seconds, and milliseconds.
Python3
from datetime import datetime as dt x = dt.now().isoformat()print('Current ISO:', x)
Output:
Current ISO: 2022-06-20T17:03:23.299672
The Python time module, allows you to work with time in Python. It provides features such as retrieving the current time, pausing the program’s execution, and so on. So, before we begin working with this module, we must first import it.
Here, we are getting the current time using the time module.
Python3
import time curr_time = time.strftime("%H:%M:%S", time.localtime()) print("Current Time is :", curr_time)
Output:
Current Time is : 16:19:13
Here we are trying to get time in milliseconds by multiplying time by 1000.
Python3
import time millisec = int(round(time.time() * 1000)) print("Time in Milli seconds: ", millisec)
Output:
Time in Milli seconds:  1655722337604
In this example, we will get time in nanoseconds using time.ns() method.
Python3
import time curr_time = time.strftime("%H:%M:%S", time.localtime()) print("Current Time is :", curr_time) nano_seconds = time.time_ns() print("Current time in Nano seconds is : ", nano_seconds)
Output:
Current Time is : 16:26:52
Current time in Nano seconds is :  1655722612496349800
Green Mean Time, which is also known as GMT can be used by using time.gmtime() method in python just need to pass the time in seconds to this method to get the GMT 
Python3
import time # current GMT Timegmt_time = time.gmtime(time.time()) print('Current GMT Time:\n', gmt_time)
Output:
Current GMT Time:
 time.struct_time(tm_year=2022, tm_mon=6, tm_mday=20, 
 tm_hour=11, tm_min=24, tm_sec=59, tm_wday=0, tm_yday=171, tm_isdst=0)
It is mostly used in file formats and operating systems. We can get the Epoch current time by converting the time.time() to an integer.
Python3
import time print("Epoch Time is : ", int(time.time()))
Output:
Epoch Time is :  1655723915
surajkumarguptaintern
Python datetime-program
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python