pythondatedatetimejulian-date

Extract day of year and Julian day from a string date


I have a string "2012.11.07" in python. I need to convert it to date object and then get an integer value of day of year and also Julian day. Is it possible?


Solution

  • First, you can convert it to a datetime.datetime object like this:

    >>> import datetime
    >>> fmt = '%Y.%m.%d'
    >>> s = '2012.11.07'
    >>> dt = datetime.datetime.strptime(s, fmt)
    >>> dt
    datetime.datetime(2012, 11, 7, 0, 0)
    

    Then you can use the methods on datetime to get what you want… except that datetime doesn't have the function you want directly, so you need to convert to a time tuple

    >>> tt = dt.timetuple()
    >>> tt.tm_yday
    312
    

    The term "Julian day" has a few different meanings. If you're looking for 2012312, you have to do that indirectly, e.g., one of the following.

    >>> int('%d%03d' % (tt.tm_year, tt.tm_yday))
    2012312
    >>> tt.tm_year * 1000 + tt.tm_yday
    2012312
    

    If you're looking for a different meaning, you should be able to figure it out from here. For example, if you want the "days since 1 Jan 4713 BC" meaning, and you have a formula that requires Gregorian year and day in year, you've got those two values above to plug in. (If you have a formula that takes Gregorian year, month, and day, you don't even need the timetuple step.) If you can't work out where to go from there, ask for further details.

    If you don't have a formula—and maybe even if you already do—your best bet is probably to look around PyPI and ActiveState for pre-existing modules. For example, a quick search turned up something called jdcal. I'd never seen it before, but a quick pip install jdcal and a brief skim of the readme, and I was able to do this:

    >>> sum(jdcal.gcal2jd(dt.year, dt.month, dt.day))
    2456238.5
    

    That's the same result that the USN Julian date converter gave me.

    If you want integral Julian day, instead of fractional Julian date, you have to decide which direction you want to round—toward 0, toward negative infinity, rounding noon up to the next day, rounding noon toward even days, etc. (Note that Julian date is defined as starting since noon on 1 Jan 4713BC, so half of 7 Nov 2012 is 2456238, the other half is 2456239, and only you know which one of those you want…) For example, to round toward 0:

    >>> int(sum(jdcal.gcal2jd(dt.year, dt.month, dt.day)))
    2456238