Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python获取日期列表?_Python_Date - Fatal编程技术网

如何使用Python获取日期列表?

如何使用Python获取日期列表?,python,date,Python,Date,所以我的这个程序很有效。我可以得到从开始日期到结束日期的日期列表。但是它有很多代码。我想知道是否有更简单的方法来做同样的事情。任何能给予的帮助都将不胜感激 start_date = 20151002 # < 10-02-2015 dates = [] end_date = 20170304 # 03-04-2017 def nextday(): global start_date while start_date < end_date: start

所以我的这个程序很有效。我可以得到从开始日期到结束日期的日期列表。但是它有很多代码。我想知道是否有更简单的方法来做同样的事情。任何能给予的帮助都将不胜感激

start_date = 20151002 # < 10-02-2015
dates = []
end_date = 20170304 # 03-04-2017 
def nextday():
    global start_date
    while start_date < end_date: 
        start_date += 1
        next_date = str(start_date)
        dates.append(start_date)
        #so with the date the first 4 characters is the year
        #the next two charcters is the month
        #The last two charcters is the day. 
        #str(start_date)[4:] should actually be the month and the day.
        if str(next_date)[4] + str(next_date)[5] == "10" and str(next_date)[6] + str(next_date)[7] == "31":#< there are only 31 days in oct - 10
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "1", "1", "0", "1"]
            print "Nov"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "11" and str(next_date)[6] + str(next_date)[7] == "30": #< there are only 30 days in nov - 11
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "1", "2", "0", "1"]
            print "Dec"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "12" and str(next_date)[6] + str(next_date)[7] == "31": #< there are only 31 days in dec - 12
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], str(int(next_date[3]) + 1), "0","1","0","1"]
            print "Jan", "New year"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "01" and str(next_date)[6] + str(next_date)[7] == "31": #< there are only 31 days in jan - 01
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","2","0","1"]
            print "Feb"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if int(next_date[0]+next_date[1]+next_date[2]+next_date[3]) % 4 == 0:
            if str(next_date)[4] + str(next_date)[5] == "02" and str(next_date)[6] + str(next_date)[7] == "29": #< there are only 29 days in LEAP YEAR feb - 02
                #^ This month will also change the year.
                date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","3","0","1"]
                print "Mar"
                new_month = ""
                for dat in date_list:   ####################LEAP YEAR#################
                    new_month += dat
                start_date = int(new_month)
        else:
            if str(next_date)[4] + str(next_date)[5] == "02" and str(next_date)[6] + str(next_date)[7] == "28": #< there are only 28 days in feb - 02
                #^ This month will also change the year.
                date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","3","0","1"]
                print "Mar"
                new_month = ""
                for dat in date_list:
                    new_month += dat
                start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "03" and str(next_date)[6] + str(next_date)[7] == "31": #< there are only 29 days in mar - 03
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","4","0","1"]
            print "Apr"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "04" and str(next_date)[6] + str(next_date)[7] == "30": #< there are only 30 days in apr - 04
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","5","0","1"]
            print "May"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "05" and str(next_date)[6] + str(next_date)[7] == "31": #< there are only 31 days in may - 05
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","6","0","1"]
            print "Jun"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "06" and str(next_date)[6] + str(next_date)[7] == "30": #< there are only 30 days in jun - 0
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","7","0","1"]
            print "Jul"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "07" and str(next_date)[6] + str(next_date)[7] == "31": #< there are only 31 days in jul - 0
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","8","0","1"]
            print "Aug"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "08" and str(next_date)[6] + str(next_date)[7] == "31": #< there are only 31 days in aug - 0
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "0","9","0","1"]
            print "Sept"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        if str(next_date)[4] + str(next_date)[5] == "09" and str(next_date)[6] + str(next_date)[7] == "30": #< there are only 31 days in sep - 0
            #^ This month will also change the year.
            date_list = [next_date[0], next_date[1], next_date[2], next_date[3], "1","0","0","1"]
            print "Oct"
            new_month = ""
            for dat in date_list:
                new_month += dat
            start_date = int(new_month)
        print start_date
nextday()
print dates
start_date=20151002#<10-02-2015
日期=[]
结束日期=20170304#2017年4月3日
def nextday():
全球开始日期
开始日期import datetime
start =  datetime.date(2015, 10, 2)
end = datetime.date(2017, 3, 4)
current = start
dates = []
while current <= end:
    dates.append(str(current))
    current += datetime.timedelta(days=1)
from datetime import date
from datetime import timedelta

one_day = timedelta(days=1)
start_date = date(2015, 10, 2)

print(start_date)
print(start_date + one_day)
import datetime

day = datetime.timedelta(days=1)
start = datetime.datetime(2015, 10, 3)
end = datetime.datetime(2017, 3, 5)
times = [start + day * x for x in range((end - start).days)]
>>> times
[datetime.datetime(2015, 10, 3, 0, 0),
 datetime.datetime(2015, 10, 4, 0, 0),

 ...

 datetime.datetime(2017, 3, 3, 0, 0),
 datetime.datetime(2017, 3, 4, 0, 0)]
import datetime

start_date = datetime.datetime(2015, 10, 02)
end_date = datetime.datetime(2017, 03, 04)
def nextday():
    global start_date
    start_date += datetime.timedelta(days=1)
    return min(start_date, end_date)

>>> start_date
datetime.datetime(2015, 10, 2, 0, 0)
>>> nextday()
datetime.datetime(2015, 10, 3, 0, 0)
>>> nextday()
datetime.datetime(2015, 10, 4, 0, 0)
>>> nextday()
datetime.datetime(2015, 10, 5, 0, 0)
>>> nextday()
datetime.datetime(2015, 10, 6, 0, 0)