Get the current date and time in every possible format in Python

Hello programmers, in this tutorial we will see how to get the current date and time in Python.

Often developers, need to use the current date and time for various manipulations. The further code depends on the date and time. Many of the big e-commerce firms conduct special events which are limited and the control is taken using the date and time.

Installation

Use the following code for installing the datetime module from the command prompt.

pip install datetime

Get the current date and time in Python

We can get the current date and time in Python using the inbuilt module ‘datetime’. We import the ‘datetime’ library and from there we call the ‘datetime.now()’ function to get the present date and time. Given below is the illustration of the default output when we call the ‘datetime.now()’ function from the library.

#Import library
import datetime

#get current date and time
def getCurrDateTime():
    currDateTime = datetime.datetime.now()
    return currDateTime

#driver
if __name__ == "__main__":
    currDateTime = str(getCurrDateTime())
    print(f"The current date and time is: {currDateTime}.")

Output

The current date and time is: 2022-09-17 18:20:33.423292.

Explanation
We import the datetime library and return the current date and time from the ‘getCurrDateTime()’ function and print it. The output denotes the date and time in ‘yyyy-mm-dd hh:mm:ss:ms’ where ms stands for milliseconds.

 Get the current date and format it: SO

Often the developers are not in need of time returned by the ‘datetime.now()’ method. The default date formatting can be changed and printed accordingly to the user’s requirements. Given below is the illustration of how to extract the date and print it.

#Import libraries
import datetime

#get current date
def getCurrDate():
    currDate = datetime.datetime.now()
    return currDate.strftime('%d/%m/%Y')

#driver
if __name__ == "__main__":
    print(f"The current date is: {getCurrDate()}.")

Output

The current date is: 17/09/2022.

Explanation

We import the datetime module and use the ‘strftime()’ method to format the datetime object and convert it into a string and print it.

There are various different pre-formatters in the datetime module with which the user can accordingly control the output of the date format.

‘a’ -- gets the week day with three letter abbreviation

‘A’ -- gets the full week day name

‘b’ -- gets the month with three letter abbreviation

‘B’ -- gets the month full name

‘y’ -- gets the year with two digits

‘Y’ -- gets the full year

Given below is an illustration of the above-mentioned formatters.

#Import library
import datetime

#driver
if __name__ == "__main__":
    currDateTime = datetime.datetime.now()
    print(f"The current date is: {currDateTime.strftime('%d/%m/%Y')}.")
    print(f"The week day with three letter abbreviation is: {currDateTime.strftime('%a')}.")
    print(f"The week day with full name is: {currDateTime.strftime('%A')}.")
    print(f"The month with three letter abbreviation is: {currDateTime.strftime('%b')}.")
    print(f"The month with full name is: {currDateTime.strftime('%B')}.")
    print(f"The year with two digit is: {currDateTime.strftime('%y')}.")
    print(f"The year with four digit is: {currDateTime.strftime('%Y')}.")

Output

The current date is: 17/09/2022.

The week day with three letter abbreviation is: Sat.

The week day with full name is: Saturday.

The month with three letter abbreviation is: Sep.

The month with full name is: September.

The year with two digit is: 22.

The year with four digit is: 2022.

Also read: Display various Datetime formats using Python

Get the current time and format it

Similar to the date formatting, we can also format the time and use it for further manipulations. Given below is an illustration of how to get the present time.

#Import libraries
import datetime

#get current time
def getCurrTime():
    currTime = datetime.datetime.now()
    return currTime.strftime('%H:%M:%S')

#driver
if __name__ == "__main__":
    print(f"The current date is: {getCurrTime()}.")

Output

The current date is: 18:48:42.

We have different formatters for time which can be changed accordingly to the user’s requirements.

‘I’ -- gets the hour in 12-hr format

‘H’ -- gets the hour in 24-hr format

‘M’ -- gets the minutes

‘S’ -- gets the seconds

‘f’ -- gets the microseconds

‘p’ -- gets the meridian

Given below is an illustration of the above-discussed formatters.

Import library
import datetime

#driver
if __name__ == "__main__":
    currDateTime = datetime.datetime.now()
    print(f"The present time is: {currDateTime.strftime('%H:%M:%S')}.")
    print(f"The hour in 12-hr format is: {currDateTime.strftime('%I')}.")
    print(f"The hour in 24-hr format is: {currDateTime.strftime('%H')}.")
    print(f"The minute is: {currDateTime.strftime('%M')}.")
    print(f"The second is: {currDateTime.strftime('%S')}.")
    print(f"The microsecond is: {currDateTime.strftime('%f')}.")
    print(f"The meridian is: {currDateTime.strftime('%p')}.")

Output

The present time is: 18:54:03.

The hour in 12-hr format is: 06.

The hour in 24-hr format is: 18.

The minute is: 54.

The second is: 03.

The microsecond is: 156736.

The meridian is: PM.

 

Using the above formatters discussed for both date and time, the user can generate any kind of formatted date and time accordingly.

Leave a Reply

Your email address will not be published.