Converting values into proper dates

58 views Asked by At

Using Python3, I have the following for loop that outputs a year followed by a day number as one figure. I want to be able to be able to convert this into a proper date. Please see below the code and what I am trying to achieve.

from datetime import datetime
import os
import rasterio

for filename in sorted(os.listdir(output_dir)):
    f = os.path.join(output_dir, filename)
    # checking if it is a file
    if os.path.isfile(f):
        #print(filename)
        with rasterio.open(f) as clipped:
            import re
            x_title = re.findall('\d+', filename)[0]
            date_object = datetime.strptime(x_title, "%Y%j")
            print(date_object)

When I run this code I get something like 2022-04-05 00:00:00 for the variable date_object which is close to what I want but there are two issues. First, there are the hours, minutes and seconds values showing up which I do not need, and second, I would then like to convert the date into a proper date such as "April 5 2022" rather than "2022-04-05".

Perhaps my lack of knowledge of the datetime module is the root cause but at this point I could use a hand. Thanks again.

0

There are 0 answers