发布网友 发布时间:2022-04-23 05:44
共4个回答
热心网友 时间:2023-10-17 02:52
# -*- coding: utf-8 -*-
import datetime
date1=datetime.datetime.strptime('2014-09-20','%Y-%m-%d')
date2=datetime.datetime.strptime('2014-10-04','%Y-%m-%d')
i=datetime.timedelta(days=1)
while i<(date2-date1):
print (date1+i).strftime('%Y-%m-%d')
i+=datetime.timedelta(days=1)
>>>
14 days, 0:00:00
2014-09-21
2014-09-22
2014-09-23
2014-09-24
2014-09-25
2014-09-26
2014-09-27
2014-09-28
2014-09-29
2014-09-30
2014-10-01
2014-10-02
2014-10-03
热心网友 时间:2023-10-17 02:52
python3编辑,未考虑间隔是偶数天的情况(间隔是偶数天的情况下,按中间两天中的前边一天作为“中间”)。
import datetime
date_strA= "2014-09-29"
date_strB= "2014-10-04"
dayA= datetime.datetime.strptime(date_strA, '%Y-%m-%d')
dayB= datetime.datetime.strptime(date_strB, '%Y-%m-%d')
sub_day=dayB-dayA
mid_day=dayA+sub_day/2
print(mid_day.strftime('%Y-%m-%d'))
热心网友 时间:2023-10-17 02:53
import datetime
#日期间隔
d1 = datetime.datetime(2014,10, 04)
d2 = datetime.datetime(2004, 9, 29)
print (d1 - d2).days
#程序运行时间
start_time = datetime.datetime.now()
time.sleep(5)
end_time = datetime.datetime.now()
print end_time-start_time追问答非所问啊,我问题的结果是应该输出:
2014-09-29
2014-09-30
2014-10-01
你上面的程序输出的是两个日期之间差几天
追答#修改后
import datetime
d1 = datetime.datetime(2005, 2, 16)
d2 = datetime.datetime(2004, 12, 31)
xDay = (d1 - d2).days
aDay = datetime.timedelta(days=1)
i = 0
while i < xDay:
i += 1
d2 += aDay
print d2.strftime('%Y-%m-%d')
热心网友 时间:2023-10-17 02:53
>>> timea='2014-09-29'
>>> timeb='2014-10-04'
>>> datetime.datetime.strptime(timea,'%Y-%m-%d')
datetime.datetime(2014, 9, 29, 0, 0)
>>> aaa=datetime.datetime.strptime(timea,'%Y-%m-%d')
>>> bbb=datetime.datetime.strptime(timeb,'%Y-%m-%d')
>>> aaa-bbb
datetime.timedelta(-5)
>>> bbb-aaa
datetime.timedelta(5)
>>>