Search code examples
pythondatedatetimepython-datetimestrptime

How to parse an 8 digit number as a date?


I need to build a way to automate file names, and I was curious as to if there is a function or a quick way to take in 8 digits and output the corresponding Date.

Specifically I only need the Month and year.

Example: 03232021 -> Mar2021

I was trying pandas to_datetime but it didn't seem like it was what I needed.


Solution

  • In [133]: s = "03232021"                                                                                                                                                                                                                                                      
    
    In [134]: dt.datetime.strptime(s, "%m%d%Y").date()                                                                                                                                                                                                                            
    Out[134]: datetime.date(2021, 3, 23)