Getting the Current Date and Time

To get both the current Date and Time datetime.now() method is used. It returns the local current date and time.

Syntax:

datetime.now(tz)

Example:

Python3




# 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 : 2021-03-16 17:59:03.837572

The now() function has various attributes that can give the desired detail from the above output. Some of the attributes are year, month, date, hour, minute, second. See the below example for a better understanding.

Example:

Python3




# 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 : 2021
Month : 3
Day : 16
Hour : 18
Minute : 1
Second : 59
Microsecond : 490402

Getting only current Date

DateTime module also provides another method called today() that only prints the value of today’s date.

Example:

Python3




# Python program to get 
# current date 
  
  
# Import date class from datetime module 
from datetime import date 
  
  
# Returns the current local date 
today = date.today() 
print("Today date is: ", today)


Output:

Today date is:  2021-03-16

Getting Only current Time

We can create a time object using the time() function. Consider the below example.

Example:

Python3




from datetime import datetime 
  
# Time object containing 
# the current time. 
time = datetime.now().time() 
  
print("Current Time =", time)


Output:

Current Time = 18:13:35.003918

Refer to the below articles to get detailed information about getting the current date and time.

Manipulate Date and Time with the Datetime Module in Python

Have you ever wondered about working with Date and Time with Python? If you have then you must have noticed that Python does not provide any built-in method to work with either Date or Time. But thanks to the DateTime module that comes pre-loaded with Python’s standard utility modules we can easily manipulate date and time according to our own need. We can even perform operations like getting a current date, adding or subtracting date and time, and much more.

In this article, we will learn all the operations that can be performed on the Date and Time using the DateTime module in Python. So before starting let’s see the basics of the DateTime module and the classes it contains.

The datetime classes are categorized into 6 main classes –

  • date – An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Its attributes are year, month and day.
  • time – An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds. Its attributes are hour, minute, second, microsecond, and tzinfo.
  • datetime – Its a combination of date and time along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
  • timedelta – A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.
  • tzinfo – It provides time zone information objects.
  • timezone – A class that implements the tzinfo abstract base class as a fixed offset from the UTC (New in version 3.2).

Similar Reads

Getting the Current Date and Time

To get both the current Date and Time datetime.now() method is used. It returns the local current date and time....

Formatting Date and Time

...

Handling timedelta objects

...

Comparing Dates

...

Working with Different Timezones

...