Python Question: Year and Day of Year to date?

前端 未结 5 429
面向向阳花
面向向阳花 2020-11-27 17:13

I have a year value and a day of year and would like to convert to a date (day/month/year).

相关标签:
5条回答
  • 2020-11-27 18:02

    The toordinal() and fromordinal() functions of the date class could be used:

    from datetime import date
    date.fromordinal(date(year, 1, 1).toordinal() + days - 1)
    
    0 讨论(0)
  • 2020-11-27 18:04
    >>> import datetime
    >>> datetime.datetime.strptime('2010 120', '%Y %j')
    datetime.datetime(2010, 4, 30, 0, 0)
    >>> _.strftime('%d/%m/%Y')
    '30/04/2010'
    
    0 讨论(0)
  • 2020-11-27 18:05
    >>>import datetime
    >>>year = int(input())
    >>>month = int(input())
    >>>day = int(input())
    data = datetime.datetime(year,month,day)
    daynew = data.toordinal()
    yearstart = datetime.datetime(year,1,1)
    day_yearstart = yearstart.toordinal()
    print ((daynew-day_yearstart)+1)
    
    0 讨论(0)
  • 2020-11-27 18:09
    datetime.datetime(year, 1, 1) + datetime.timedelta(days - 1)
    
    0 讨论(0)
  • 2020-11-27 18:11

    2020 Edition:

    import mx.DateTime as dt 
    date = dt.DateTime(yyyy,mm,dd) + dt.DateTimeDeltaFromDays(doy-1)
    

    So, given that you know the year (say, 2020) and the doy (day of the year, say 234), then:

    date = dt.DateTime(2020,1,1) + dt.DateTimeFromDays(233) 
    

    which returns

    2020-08-21 00:00:00.00
    
    0 讨论(0)
提交回复
热议问题