Python Datetime and Formatting

Posted August 9, 2022 by Rohith ‐ 2 min read

In python, it is possible to fetch the current datetime and timestamp using inbuilt libraries like datetime, time and pendulum.

Current Date time

In this article, we will use python datetime package to get the current datetime, current date only, current time only and current timestamp.

Using python datetime package

Import datetime module from datetime package to get current datetime.

Example: In the example, it returns a datetime class object containing the current date & time information in provided timezone. If timezone is None then returned object will contain the current date time information in local timezone.

from datetime import datetime
current_datetime = datetime.now(tz=None)
# datetime.now() is equivalent to datetime.now(tz=None)
print(current_date)

Output:

2022-08-11 11:35:45.239425

Extract Parts of Date

You can extract the parts of date using datetime object attributes.

Let the current_datetime be the datetime object.

Example: below code creates current_datetime object.

Example:

from datetime import datetime
date_time_obj = datetime.now()

Output:

2022-08-11 11:35:45.239425

Extract Day

We can extract day using attribute day.

date_time_obj.day # returns: 11

Extract Month

We can extract month using attribute month.

date_time_obj.month # returns: 8

Extract Hour

We can extract hour using attribute hour.

date_time_obj.hour # returns: 11

Extract Minute

We can extract minute using attribute minute.

date_time_obj.minute # returns: 35

Extract Second

We can extract second using attribute second.

date_time_obj.second # returns: 45

Format By Extracted Parts

# Access the member variables of datetime object to print date & time information
print(date_time_obj.year, '/', date_time_obj.month, '/', date_time_obj.day)
print(date_time_obj.hour, ':', date_time_obj.minute, ':', date_time_obj.second, '.', date_time_obj.microsecond)

Get Datetime Format In String

Instead of accessing each member of datetime object like above, we can directly convert the datetime object to interested string formats.

Example:

timestamp_str = date_time_obj.strftime('%Y-%m-%dT%H:%M:%S.%f')
print('Current Timestamp: ', timestamp_str)

output:

Current Timestamp:  2022-08-11T11:35:45.239425

Create Datetime Object From Datetime String

We can easily convert the datetime string to datetime object using strptime method.

For example, we want to convert 2022-08-11T11:35:45.239425 into a datetime object.

Example:

from datetime import datetime

datetime_str = '2022-08-11T11:35:45.239425'
datetime_obj = datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S.%f')

Get Current Date Only

It is also possible to get date only from datetime object using date() method.

Example:

date_obj = datetime_obj.date() 
# returns: datetime.date(2022, 8, 11)

It is simple to get date string from date object.

Example:

date_str = str(datetime_obj.date()) 
# returns: '2022-08-11'

Get Current Time Only

It is also possible to get date only from datetime object using time() method.

Example:

time_obj = datetime_obj.time() 
# returns: datetime.time(11, 42, 20, 682356)

It is simple to get time string from time object.

Example:

time_str = str(datetime_obj.time()) 
# returns: '11:42:20.682356'

Subscribe For More Content