Convert Year/Month/Day to Day of Year in Python

前端 未结 6 1596
不知归路
不知归路 2020-11-29 17:32

I\'m using the datetime module, i.e.:

>>> import datetime
>>> today = datetime.datetime.now()
>>> print(today)
2009-03-06 13:24:58.         


        
相关标签:
6条回答
  • 2020-11-29 17:40

    DZinX's answer is a great answer for the question. I found this question and used DZinX's answer while looking for the inverse function: convert dates with the julian day-of-year into the datetimes.

    I found this to work:

    import datetime
    datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S')
    
    >>>> datetime.datetime(1936, 3, 17, 13, 14, 15)
    
    datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S').timetuple().tm_yday
    
    >>>> 77
    

    Or numerically:

    import datetime
    year,julian = [1936,77]
    datetime.datetime(year, 1, 1)+datetime.timedelta(days=julian -1)
    
    >>>> datetime.datetime(1936, 3, 17, 0, 0)
    

    Or with fractional 1-based jdates popular in some domains:

    jdate_frac = (datetime.datetime(1936, 3, 17, 13, 14, 15)-datetime.datetime(1936, 1, 1)).total_seconds()/86400+1
    display(jdate_frac)
    
    >>>> 77.5515625
    
    year,julian = [1936,jdate_frac]
    display(datetime.datetime(year, 1, 1)+datetime.timedelta(days=julian -1))
    
    >>>> datetime.datetime(1936, 3, 17, 13, 14, 15)
    

    I'm not sure of etiquette around here, but I thought a pointer to the inverse functionality might be useful for others like me.

    0 讨论(0)
  • 2020-11-29 17:41

    If you have reason to avoid the use of the datetime module, then these functions will work.

    def is_leap_year(year):
        """ if year is a leap year return True
            else return False """
        if year % 100 == 0:
            return year % 400 == 0
        return year % 4 == 0
    
    def doy(Y,M,D):
        """ given year, month, day return day of year
            Astronomical Algorithms, Jean Meeus, 2d ed, 1998, chap 7 """
        if is_leap_year(Y):
            K = 1
        else:
            K = 2
        N = int((275 * M) / 9.0) - K * int((M + 9) / 12.0) + D - 30
        return N
    
    def ymd(Y,N):
        """ given year = Y and day of year = N, return year, month, day
            Astronomical Algorithms, Jean Meeus, 2d ed, 1998, chap 7 """    
        if is_leap_year(Y):
            K = 1
        else:
            K = 2
        M = int((9 * (K + N)) / 275.0 + 0.98)
        if N < 32:
            M = 1
        D = N - int((275 * M) / 9.0) + K * int((M + 9) / 12.0) + 30
        return Y, M, D
    
    0 讨论(0)
  • 2020-11-29 17:43

    You could use strftime with a %j format string:

    >>> import datetime
    >>> today = datetime.datetime.now()
    >>> today.strftime('%j')
    '065'
    

    but if you wish to do comparisons or calculations with this number, you would have to convert it to int() because strftime() returns a string. If that is the case, you are better off using DzinX's answer.

    0 讨论(0)
  • 2020-11-29 17:45

    Just subtract january 1 from the date:

    import datetime
    today = datetime.datetime.now()
    day_of_year = (today - datetime.datetime(today.year, 1, 1)).days + 1
    
    0 讨论(0)
  • 2020-11-29 17:48

    I want to present performance of different approaches, on Python 3.4, Linux x64. Excerpt from line profiler:

          Line #      Hits         Time  Per Hit   % Time  Line Contents
          ==============================================================
             (...)
             823      1508        11334      7.5     41.6          yday = int(period_end.strftime('%j'))
             824      1508         2492      1.7      9.1          yday = period_end.toordinal() - date(period_end.year, 1, 1).toordinal() + 1
             825      1508         1852      1.2      6.8          yday = (period_end - date(period_end.year, 1, 1)).days + 1
             826      1508         5078      3.4     18.6          yday = period_end.timetuple().tm_yday
             (...)
    

    So most efficient is

    yday = (period_end - date(period_end.year, 1, 1)).days
    
    0 讨论(0)
  • 2020-11-29 17:53

    Use datetime.timetuple() to convert your datetime object to a time.struct_time object then get its tm_yday property:

    from datetime import datetime
    day_of_year = datetime.now().timetuple().tm_yday  # returns 1 for January 1st
    
    0 讨论(0)
提交回复
热议问题