close

How to extract only the month and day from a datetime object?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to extract only the month and day from a datetime object in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to extract only the month and day from a datetime object?

  1. How to extract only the month and day from a datetime object?

    Your approach to converting the date string into a datatime object is correct. It should normally contain as much information as possible.

  2. extract only the month and day from a datetime object

    Your approach to converting the date string into a datatime object is correct. It should normally contain as much information as possible.

Method 1

Your approach to converting the date string into a datatime object is correct. It should normally contain as much information as possible.

You are then hoping to only use the day and month for plotting your time based data using matplotlib. The usual approach though is to first convert your dates into datatime objects (which you have done), and to then get matplotlib to convert these values into its own internal representation using the date2num() function.

Give this function the whole datetime object including the year. If your data happened to span more than 12 months (or if it crossed from December to January) you would need it, even if you don’t wish it to be displayed.

Next, you can tell matplotlib how to format any ticks on the x-axis using a formatter, in this case a DateFormatter(). So you could for example choose to display month and day for each tick.

In this example, I display the month, with the day on the line below:

from matplotlib import pyplot, dates
from datetime import datetime

data = [("19800801", 16), ("19800810", 10), ("19800901", 15), ("19800905", 14)]

xaxis = [datetime.strptime(d, '%Y%m%d') for d, v in data]
yaxis = [v for d, v in data]

ax = pyplot.gca()
xaxis = dates.date2num(xaxis)    # Convert to maplotlib format
hfmt = dates.DateFormatter('%m\n%d')
ax.xaxis.set_major_formatter(hfmt)

pyplot.xlabel('Date')
pyplot.ylabel('Value')
pyplot.plot(xaxis, yaxis)
pyplot.tight_layout()

pyplot.show()

This would display as follows:

month day xaxis matplotlib demo

You could then extend this to use a DayLocator() which tells matplotlib to place the xticks at exact day locations as follows:

ax.xaxis.set_major_locator(dates.DayLocator())

Giving you:

plot using day locator

Method 2

This will take an integer (supposing it is a valid epoch) and convert it into a python datetime object:

>>> import datetime
>>> date = datetime.datetime.fromtimestamp(19800801)
>>> print date.month
8
>>> print date.day
17

or using strftime (returns string and not a datetime object):

>>> datetime.datetime.fromtimestamp(19800801).strftime('%m%d')
'0817'

outputting the month and day as a single integer:

>>> int(datetime.datetime.fromtimestamp(19800801).strftime('%m%d'))
817

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read