Skip to content

Extracting Datetime Format in Python: Day, Date, Month, Year, etc.

Extracting data in datetime format can be very tricky and frustrating. I have summed up a few things about datetime in Python that I have learned in the code below.

Let’s load the packages that we are going to need, such as pandas and datetime, then, define a variable to store a datetime data format.

import pandas as pd
from datetime import datetime

my_datetime = "2021-02-17 06:59:51"

Extracting Time (Hour, Minute, Second) From A String

To extract the hour, minute, and second, it’s pretty straightforward.

hour = pd.to_datetime(my_datetime).hour
minute = pd.to_datetime(my_datetime).minute
second = pd.to_datetime(my_datetime).second

(6, 59, 51) # Your output should look like this

Extracting Date From A String

You probably get the idea now that to extract the datetime component in a variable you can use pd.to_datetime(). You get use the same thing to extract the date, month, year.

date = pd.to_datetime(my_datetime).date()
day = pd.to_datetime(my_datetime).day
day_name = pd.to_datetime(my_datetime).day_name()
month = pd.to_datetime(my_datetime).month
month_name = pd.to_datetime(my_datetime).month_name()
year = pd.to_datetime(my_datetime).year

print("date: " + str(date))
print("date of the month: " + str(day))
print("day of the week: " + day_name)
print("month: " + str(month))
print("month name: " + month_name)
print("year: " + str(year))
date: 2021-02-17 
date of the month: 17 
day of the week: Wednesday 
month: 2 month 
name: February 
year: 2021

Extracting Date From A Data Frame Column

Now what if your data are in a data frame column? The above codes wouldn’t work on data from a data frame column. So here’s what you should use instead, but first let’s build a data frame first. The sequence column is just something random I came up with so the data frame can have more than one column.

df = pd.DataFrame({"sequence":[100, 200, 300],
                   "datetime":["2021-02-17 06:59:59", 
                               "2021-01-15 02:19:59", 
                               "2021-01-02 10:15:18"]})

Here’s the code to extract all the information. Notice the dt used here. The first line of code is used to extract the date only, meanwhile the second one shows you how to extract the month and year.

df['date'] = pd.to_datetime(df['datetime'])
df['month'] = pd.to_datetime(df['datetime']).dt.to_period('M')


Extracting Time Using Strftime

You can also do so much more using strftime.

Be careful with the capitalization. The example below uses %b to get the month names, which are all in the shortened format (Jan, Feb, Mar, and so on). If you use %B you will get the full month’s names (January, February, March, and so on). Also, %D will give you full date (17/02/2021) instead of just the date (17). Play around with these to get more understanding on strftime formatting.

date1 = pd.to_datetime(df['datetime']).dt.strftime("%d-%b-%Y")
time1 = pd.to_datetime(df['datetime']).dt.strftime("%H:%M:%S")

If you want to learn more about this, you can read the official documentation on Python’s datetime using the link here.


Leave a Reply

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

%d bloggers like this: