Skip to content
Related Articles

Related Articles

Get Current Date and Time using Python
  • Difficulty Level : Easy
  • Last Updated : 27 Dec, 2019

Prerequisite: datetime module

In Python, date and time are not a data type of its 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.

Syntax : datetime.now(tz)

Parameters :
tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.)



Returns : Returns the current date and time in time format.

Example 1:




# Python3 code to demonstrate 
# Getting current date and time using  
# now(). 
    
# importing datetime module for now() 
import datetime 
    
# using now() to get current time 
current_time = datetime.datetime.now() 
    
# Printing value of now. 
print ("Time now at greenwich meridian is : "
                                    , end = "") 
print (current_time) 

Output:

Time now at greenwich meridian is : 2019-12-11 13:54:30.967356

Attributes of now() :

now() has different attributes, same as attributes of time such as year, month, date, hour, minute, second.

Example 2:




# Python3 code to demonstrate 
# attributes of now() 
    
# importing datetime module for now() 
import datetime 
    
# using now() to get current time 
current_time = datetime.datetime.now() 
    
# Printing attributes of now(). 
print ("The attributes of now() are : "
    
print ("Year : ", end = "") 
print (current_time.year) 
    
print ("Month : ", end = "") 
print (current_time.month) 
    
print ("Day : ", end = "") 
print (current_time.day) 
    
print ("Hour : ", end = "") 
print (current_time.hour) 
    
print ("Minute : ", end = "") 
print (current_time.minute) 
    
print ("Second : ", end = "") 
print (current_time.second) 
    
print ("Microsecond : ", end = "") 
print (current_time.microsecond) 

Output:

The attributes of now() are : 
Year : 2019
Month : 12
Day : 11
Hour : 13
Minute : 56
Second : 7
Microsecond : 292616

Getting time of particular timezone :

In the above example, it can be seen that above code does not gives 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.

Example: 3




# Python3 code to demonstrate 
# attributes of now() for timezone 
    
# for now() 
import datetime 
    
# for timezone() 
import pytz 
    
# using now() to get current time 
current_time = datetime.datetime.now(pytz.timezone('Asia/Kolkata')) 
    
# printing current time in india 
print ("The current time in india is : "
print (current_time)  

Output:

The current time in india is : 
2019-12-11 19:28:23.973616+05:30

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :