Python Date Time

Now The current date and time as shown in our computer system can be known using the following:

  • ctime() method of 'time' module
  • now() method of 'datetime' class of 'datetime' module
  • today() method of 'datetime' class of 'datetime' module

When ctime() is used without passing any epoch time, it returns the current date and time in string format.

Python Program to get Current Date and Time

#finding current date and time using ctime() 
import time 
t = time.ctime() 
print(t)

To know the current date and time, we can also take the help of now() method of 'datetime' class that belongs to 'datetime' module. This method returns an object that contains date and time information in any timezone. The time zone information should be provided to this method. If the time zone is not provided, then it takes the local time zone. For example, when we use this method in India, it gives the date and time according to IST (India Standard Time) that is 5.30 hours ahead of Greenwich Mean Time. This is called local time zone of India.

Python Program Date Time Format Example

#finding current date and time 
from datetime import * 
now = datetime.now() 
print(now) 
print('Date now: {}/{}/{}'.format(now.day, now.month, now.year)) 
print('Time now: {}:{}:{}'.format(now.hour, now.minute, now.second))

Python Program to get Today Date and Time

#finding today's date and time 
from datetime import * 
#today() of datetime class gives date and time 
tdm = datetime.today() 
print("Today's date and time= ",tdm) 
#today() of date class gives the date only 
td = date.today() 
print("Today's date=", td)