import datetime
def getDays(year):
"""Generates all days in one year, respecting leap years. Uses Gregorian
calendarso best keep the year post 1582."""
def isLeap(y):
return y%4 == 0 and ( not y % 100 == 0 or y % 400 == 0)
dt = datetime.date(year,1,1)
days = 365 + (1 if isLeap(dt.year) else 0) # needs the (..) due to operator precedence
return (dt + datetime.timedelta(days=k) for k in range( days ) )
print(list(getDays(2018)))
datelist = [datetime.fromordinal(date(year, 1, 1).toordinal() + d - 1).strftime("dd.mm.yyyy") for d in range(1,365+ 1 if isLeap(year) else 0))]
分解一下,上面还有很长的路要走:
datelist = []
for d in range(1,365+ 1 if isLeap(year) else 0):
day = datetime.fromordinal(date(year, 1, 1).toordinal() + d - 1)
datelist.append(day.strftime("dd.mm.yyyy"))
from datetime import date
from datetime import timedelta
# get starting date 01.01.2018
startingDate = date(date.today().year, 1, 1)
currentDate = startingDate
for _ in range(365):
print(currentDate.strftime('%m.%d.%Y'))
#get next day
currentDate = currentDate + timedelta(days=1)
CREATE TABLE days(day TEXT PRIMARY KEY) WITHOUT ROWID;
INSERT INTO days(day)
WITH RECURSIVE alldays(day) AS
(VALUES ('2018-01-01')
UNION ALL
SELECT date(day, '+1 day')
FROM alldays
LIMIT 366 -- Take leap years into account
)
SELECT day FROM alldays WHERE day <= '2018-12-31';
-- List days in October:
SELECT day FROM days WHERE day BETWEEN '2018-10-01' AND '2018-10-31' ORDER BY day;
4条答案
按热度按时间vxbzzdmp1#
可以从datetime开始
2018-1-1
并添加一系列0
至364/365
天之后:输出:
跃迁年份:
输出:
nimxete22#
这看起来比他要找的要干净一点,而且是一条直线!
这将导致
datelist
成为一个list
从[1, 2, 3, ... 365/366]
.编辑帕特里克的建议,并根据需要更正格式
所有你需要做的是得到你想要的格式将得到从这里解释的数字日期和格式正确。这也可以在一行中完成,但对一些人来说有点多。
分解一下,上面还有很长的路要走:
这将产生如下列表:
km0tfn4u3#
eyh26e7m4#
使用sqlite,您可以直接在sql中执行此操作,无需python,这得益于递归CTE: