0

I have essentially the opposite question as this question. I have a string structured as "%y%j" such that January 1 2017 would be "17001" and December 31 1995 would be "95365".

I essentially need to know what month it is from any given input string. I thought this would be relatively simple. Something like:

input = "95365"
year = int(input[0:2])
day = int(input[2:5])
if day < 32:
    month = 1
if day >= 32 and day < 50:
    # etc...

what I failed to remember was leap years and how often they repeat. Does anyone have an easy fix? Is there a library for this sort of thing?

Community
  • 1
  • 1
Frank
  • 735
  • 1
  • 12
  • 33

1 Answers1

3

Unless I am missing something, I think all you need is:

>>> import datetime
>>> s = '95365'
>>> datetime.datetime.strptime(s, '%y%j').month
12
>>> datetime.datetime.strptime(s, '%y%j').day
31
>>> datetime.datetime.strptime(s, '%y%j').year
1995
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284