Extract day of year and Julian day from a string date

前端 未结 9 2019
臣服心动
臣服心动 2020-11-27 06:51

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

9条回答
  •  孤城傲影
    2020-11-27 06:59

    According to this article there is an unpublished one-line formula created by Fliegel and Van Flandern to calculate an Gregorian Date to an Julian Date:

    JD = 367 * year - 7 * (year + (month + 9)/12)/4 - 3 * ((year + (month - 9)/7)/100 + 1)/4 + 275 * month/9 + day + 1721029
    

    This was compacted by P. M. Muller and R. N. Wimberly of the Jet Propulsion Laboratory, Pasadena, California for dates after March of 1900 to:

    JD = 367 * year - 7 * (year + (month + 9)/12)/4 + 275 * month/9 + day + 1721014
    

    These formulas are off by 0.5, so just subtract 0.5 from the formulas.

    Use some string manupulation to actually extract the data and you will be good

    >>> year, month, day = map(int,"2018.11.02".split("."))
    >>> 367 * year - 7 * (year + (month + 9)/12)/4 + 275 * month/9 + day + 1721014 - 0.5
    2458424.5
    

提交回复
热议问题