Program to find current Date and Time using Python

by | Jun 6, 2021 | Python Programs

Home » Python » Python Programs » Program to find current Date and Time using Python

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 to find current Date and 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:

Program to find current Date and Time Output 2

Approach 2: Using pytz

Program to find current Date and Time using Python

Output:

Program to find current Date and Time output 3

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.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author