Python 为两个日期创建一个循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14288498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a loop for two dates
提问by f.ashouri
Possible Duplicate:
Iterating through a range of dates in Python
可能的重复:
在 Python 中迭代一系列日期
I have two entries:
我有两个条目:
date1 = 2004.09.25
date2 = 2004.10.08
I want to wrote a script in Python that recognizes the date range and print them. something like this:
我想用 Python 编写一个脚本来识别日期范围并打印它们。像这样:
for i in range(date1:date2):
print i
Do I need to define the dates in a particular format for dates? The expected output is like:
我是否需要为日期定义特定格式的日期?预期的输出是这样的:
2004.09.25
2004.09.26
.
.
.
2004.10.08
采纳答案by forivall
Your first step should have been to look at the python datetime library.
您的第一步应该是查看 python datetime 库。
Overall, your first solution could look something like this:
总的来说,您的第一个解决方案可能如下所示:
date1 = datetime.date(2004, 9, 25)
date2 = datetime.date(2004, 10, 8)
day = datetime.timedelta(days=1)
while date1 <= date2:
print date1.strftime('%Y.%m.%d')
date1 = date1 + day
(one thing to note: this will obviously clobber your date1variable)
(要注意的一件事:这显然会破坏您的date1变量)
I would later refactor this into a daterange function so that you can do something closer to what you did; it would look like
我稍后会将其重构为 daterange 函数,以便您可以做一些更接近您所做的事情;它看起来像
for d in daterange(date1, date2):
print d.strftime('%Y.%m.%d')
Later on, when you develop your python skills, it could like like this:
稍后,当您开发 Python 技能时,它可能是这样的:
for i in range((date2 - date1).days + 1):
print (date1 + datetime.timedelta(days=i)).strftime('%Y.%m.%d')
Or this, which would be my final version:
或者这个,这将是我的最终版本:
def daterange(d1, d2):
return (d1 + datetime.timedelta(days=i) for i in range((d2 - d1).days + 1))
for d in daterange(date1, date2):
print d.strftime('%Y.%m.%d')
回答by Volatility
Use the builtin datetimemodule.
使用内置datetime模块。
Firstly, split your dates (assuming it is a string) by each .and pass the lists to datetime.date
首先,按每个拆分日期(假设它是一个字符串).并将列表传递给datetime.date
date1 = datetime.date(*date1.split('.'))
date2 = datetime.date(*date2.split('.'))
Then get the ordinalof each of the dates (number of days since Jan 1 0001)
然后获取ordinal每个日期的(自 0001 年 1 月 1 日以来的天数)
date1 = date1.toordinal()
date2 = date2.toordinal()
Then, use a forloop, and the fromordinalfunction to convert an ordinal to a date
然后,使用for循环,以及fromordinal将序数转换为日期的函数
for i in range(date1, date2 + 1):
print date.fromordinal(i).strftime('%Y.%m.%d')
Putting it all together:
把它们放在一起:
date1 = datetime.date(*date1.split('.')).toordinal()
date2 = datetime.date(*date2.split('.')).toordinal()
for i in range(date1, date2 + 1):
print date.fromordinal(i).strftime('%Y.%m.%d')
回答by William
using the ordinal functions it would go something like this:
使用序数函数它会是这样的:
import datetime
DATE1 = datetime.date(2004,9,25)
DATE2 = datetime.date(2004,10,8)
D1 = datetime.date.toordinal(DATE1)
D2 = (datetime.date.toordinal(DATE2))+1
for i in range(D1,D2):
print datetime.date.fromordinal(i)

