How to get current date and time in Python

How do I get the current date and time in Python? What is the python library and function should I need to use to get the current time or date in Python programming language. Moreover, how can I get the current date and time in different formats using strftime() method.

In this tutorial, you will see how to get today’s date and current date and time in Python. There are a number of ways to get the current date. However, we use now() function from the datetime Python module. Also, we will see how to format the date and time in different formats using strftime() method.

This tutorial examples has tested in Python 2.7 and Python 3.7.

Get Current Date and Time in Python

The now() function is defined under the datetime module. Default, now() function Return the current local date and time in YYYY-MM-DD HH:MM:SS:MS format.

from datetime import datetime

now = datetime.now()
print("Current Date and Time: ", now)
Outputs

Current Date and Time: 2020-02-01 17:00:30.578822

Python Get Today’s Date

In the following example, we imported the datetime class from the datetime module. Next, we used the date() function to get the current local date.

from datetime import datetime

today = datetime.now().date()
print("Today's date:", today)
Outputs

Today’s date: 2020-02-01

Python Get Current Time

Similarly, we used the time() function to get the current local time.

from datetime import datetime

time = datetime.now().time()
print("Current Time:", time)
Outputs

Current Time: 17:02:19.457591

Python Get Current Date Time Attributes

You can get the specific attributes from date and time object. For example, you can get today’s date, current month or year, etc. You can see the below examples to get date time attributes from datetime object.

from datetime import datetime

now = datetime.now()
print("Current Day is:", now.day)
print("Current Month is:", now.month)
print("Current Year is:", now.year)
print("Current Hour is:", now.hour)
print("Current Minute is:", now.minute)
print("Current Second is:", now.second)
print("Current Microsecond is:", now.microsecond)
Sample Outputs

Current Day is: 1
Current Month is: 2
Current Year is: 2020
Current Hour is: 17
Current Minute is: 3
Current Second is: 8
Current Microsecond is: 556483

Get Custom Formatted Current Date Time in Python

You can get date and time in a specific format using predefined directive to format date and time. The below sample snippet you can see different formatted date and time will get from datetime object.

from datetime import datetime

now = datetime.now()
print (now.strftime("%Y-%m-%d %H:%M:%S"))
print (now.strftime("%Y-%m-%d %I:%M:%S %p"))
print (now.strftime("%a, %b %d, %Y"))
print (now.strftime("%Y/%m/%d"))
print (now.strftime("%I:%M:%S %p"))
print (now.strftime("%H:%M:%S"))
Sample Outputs

2020-02-01 17:09:52
2020-02-01 05:09:52 PM
Sat, Feb 01, 2020
2020/02/01
05:09:52 PM
17:09:52

You can use the following list of directives to format the date and time with the strftime method.

Directive Meaning
%a Weekday name.
%A Full weekday name.
%b Abbreviated month name.
%B Full month name.
%c Appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Appropriate date representation.
%X Appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal ‘%’ character.

For more reference check it out datetime Objects in the datetime module from Python Standard Library Reference.

Do you need expert help with your Python assignments in university or college? Visit here AssignmentCore and pay someone to do Python homework for you. – Advertisements

We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new WordPress articles for free. Moreover, you can explore here other Python related articles.

Note: Icon made by the more.

If you like our article, please consider buying a coffee for us.
Thanks for your support!

Support us on Buy me a coffee! Buy me a coffee!



Join the Discussion.