用Python将年/月/日转换为年。

时间:2021-12-18 21:22:41

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

我使用的是Python的“datetime”模块,即:

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

and I would like to compute the day of year that is sensitive of leap years. e.g. oday (March 6, 2009) is the 65th day of 2009. Here's web-based DateTime calculator.

我想计算的是闰年的敏感年份。例如,oday(2009年3月6日)是2009年的第65天。这是基于web的DateTime计算器。

Anyway, I see a two options:

无论如何,我看到了两种选择:

  1. Create a number_of_days_in_month array = [31, 28, ...], decide if it's a leap year, manually sum up the days

    创建number_of_days_in_month数组=[31,28,…[参考译文]决定这是一个闰年,手工总结一下天数。

  2. Use datetime.timedelta to make a guess & then binary search for the correct day of year:

    使用datetime。timedelta做一个猜测,然后二分查找正确的日期:

.

>>> import datetime
>>> YEAR = 2009
>>> DAY_OF_YEAR = 62
>>> d = datetime.date(YEAR, 1, 1) + datetime.timedelta(DAY_OF_YEAR - 1)

These both feel pretty clunky & I have a gut feeling that there's a more "Pythonic" way of calculating day of year. Any ideas/suggestions?

这两种感觉都很笨拙,我有一种直觉,认为有一种更“python化”的计算方式。意见/建议吗?

7 个解决方案

#1


178  

There is a very simple solution:

有一个非常简单的解决方案:

day_of_year = datetime.now().timetuple().tm_yday

#2


24  

Couldn't you use strftime?

你不能使用strftime吗?

>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 15:37:02.484000
>>> today.strftime('%j')
'065'

Edit

As noted in the comments, 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.

正如注释中所指出的,如果您希望对这个数字进行比较或计算,您将不得不将其转换为int(),因为strftime()返回一个字符串。如果是这样的话,你最好使用DzinX的答案。

#3


10  

DZinX's answer is a great answer for the question. I found this question while looking for the inverse function. I found this to work :

DZinX的回答是这个问题的一个很好的答案。我在寻找逆函数时发现了这个问题。我发现这是可行的:

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

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

我不太确定这里的礼仪,但我认为一个指向逆函数的指针可能对像我这样的人有用。

#4


6  

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

我想展示不同方法的性能,在Python 3.4中,Linux x64。这是*同志会见分析器行:

      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

#5


5  

Just subtract january 1 from the date:

从日期上减去1月1日:

import datetime
today = datetime.datetime.now()
day_of_year = (today - datetime.datetime(today.year, 1, 1)).days + 1

#6


2  

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

如果您有理由避免使用datetime模块,那么这些函数将会起作用。

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

#7


1  

This code takes a date as an input argument and returns the day of the year.

该代码将日期作为输入参数,并返回一年的日期。

from datetime import datetime
date = raw_input("Enter date: ")  ## format is 02-02-2016
adate = datetime.datetime.strptime(date,"%d-%m-%Y")
day_of_year = adate.timetuple().tm_yday
day_of_year

#1


178  

There is a very simple solution:

有一个非常简单的解决方案:

day_of_year = datetime.now().timetuple().tm_yday

#2


24  

Couldn't you use strftime?

你不能使用strftime吗?

>>> import datetime
>>> today = datetime.datetime.now()
>>> print today
2009-03-06 15:37:02.484000
>>> today.strftime('%j')
'065'

Edit

As noted in the comments, 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.

正如注释中所指出的,如果您希望对这个数字进行比较或计算,您将不得不将其转换为int(),因为strftime()返回一个字符串。如果是这样的话,你最好使用DzinX的答案。

#3


10  

DZinX's answer is a great answer for the question. I found this question while looking for the inverse function. I found this to work :

DZinX的回答是这个问题的一个很好的答案。我在寻找逆函数时发现了这个问题。我发现这是可行的:

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

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

我不太确定这里的礼仪,但我认为一个指向逆函数的指针可能对像我这样的人有用。

#4


6  

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

我想展示不同方法的性能,在Python 3.4中,Linux x64。这是*同志会见分析器行:

      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

#5


5  

Just subtract january 1 from the date:

从日期上减去1月1日:

import datetime
today = datetime.datetime.now()
day_of_year = (today - datetime.datetime(today.year, 1, 1)).days + 1

#6


2  

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

如果您有理由避免使用datetime模块,那么这些函数将会起作用。

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

#7


1  

This code takes a date as an input argument and returns the day of the year.

该代码将日期作为输入参数,并返回一年的日期。

from datetime import datetime
date = raw_input("Enter date: ")  ## format is 02-02-2016
adate = datetime.datetime.strptime(date,"%d-%m-%Y")
day_of_year = adate.timetuple().tm_yday
day_of_year