Table of Contents
Introduction
The task is to find the current local date and time.
Program
Approach 1: Using now()
Program 1:
import datetime current_time = datetime.datetime.now() print("The current time at greenwich meridian is: ", current_time)
Output:
Program 2:
import datetime currTime = datetime.datetime.now() print( "The current date in the format dd/mm/yyyy is :") print(currTime.day,"-",currTime.month,"-",currTime.year) print("The current time in hour:minute:second:microsecond format is ") print(currTime.hour,":",currTime.minute,":",currTime.second,":", currTime.microsecond)
Output:
Approach 2: Using pytz
Output:
Explanation
In approach 1 we have used now() function to get the current date and time. The attributes of now() function is showed in Program 2 i.e. day, month, year, hour, minutes, second and microsecond. In approach 2, we have used pytz to get the current date and time of a particular timezone. In our code we have retrieved the date and time in India.
0 Comments