473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to create list of dates between two dates?

Hello,
I am a beginner in python, and have a question..
I need to create a list of all dates between two dates..
Lets say beggining date= 2008-1-15
enddate= 2008-2-20
how do I get a list
dates = [2008-1-15, 2008-1-16,...,2008-2-20] ?
Feb 25 '08 #1
4 35756
bvdet
2,851 Expert Mod 2GB
Hello,
I am a beginner in python, and have a question..
I need to create a list of all dates between two dates..
Lets say beggining date= 2008-1-15
enddate= 2008-2-20
how do I get a list
dates = [2008-1-15, 2008-1-16,...,2008-2-20] ?
The datetime module is useful for this problem. Create a datetime object for the start and end dates. The following determines the number of days:
Expand|Select|Wrap|Line Numbers
  1. (end_date+datetime.timedelta(days=1)-start_date).days)
Use that value +1 for a range of days. Each date can be incremented by an integer like this:
Expand|Select|Wrap|Line Numbers
  1. start_date+datetime.timedelta(days=i)
Feb 25 '08 #2
The datetime module is useful for this problem. Create a datetime object for the start and end dates. The following determines the number of days:
Expand|Select|Wrap|Line Numbers
  1. (end_date+datetime.timedelta(days=1)-start_date).days)
Use that value +1 for a range of days. Each date can be incremented by an integer like this:
Expand|Select|Wrap|Line Numbers
  1. start_date+datetime.timedelta(days=i)

Thx for quick reply,
I understand the principle of incrementing days, but how to store each day into list or tuple?

In C I would have something like

DATES=[]
number of days=30
start_date= ...
end_date= ...

while (start_date <= end_date)
{
for(i=0; i<30; i++)
DATES[i]=start_date;

start_date++;
}
Feb 26 '08 #3
I solved it .. It look so simple now.. :)
Thx for the help..
Feb 26 '08 #4
bvdet
2,851 Expert Mod 2GB
I solved it .. It look so simple now.. :)
Thx for the help..
You are welcome. I am glad you were able to solve it. Here's what I came up with:
Expand|Select|Wrap|Line Numbers
  1. import datetime
  2.  
  3. def date_range(start, end):
  4.     r = (end+datetime.timedelta(days=1)-start).days
  5.     return [start+datetime.timedelta(days=i) for i in range(r)]
  6.  
  7. start = datetime.date(2007,01,01)
  8. end = datetime.date(2008,02,01)
  9. dateList = date_range(start, end)
  10. print '\n'.join([str(date) for date in dateList])
  11.  
  12. >>> 2007-01-01
  13. 2007-01-02
  14. 2007-01-03
  15. 2007-01-04
  16. 2007-01-05
  17. ...............
  18.  
Feb 26 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
0
by: Tony | last post by:
Aloha, I have a problem that I can't think of a (easy) solution for. I'm building a web app (asp/access) that will allow people to book a cruise on a 24 passenger yacht that goes around the...
5
by: jnikle | last post by:
I have two completely unrelated tables, one for reviews and another for pay periods. The reviews table has a review date in it, and the pay periods table is just a list of the beginnings of pay...
18
by: PC Datasheet | last post by:
An Access user saw my name in a newsgroup and sent me a request for help on a project. As part of the project, a list of the dates in a month was needed. For anyone needing a list of dates in a...
1
by: sang | last post by:
Hi how to list the inserted dates by week,month and year. That is how to select the dates by past one week. similarly the month and year of the particular filed. Thanks in Advance Sang
4
by: viniiya | last post by:
Hi All, How to Create list of Server/Domains and its User Names from the Network using C#? I tried with Netuserenum and I got only User Names for server but not Domain users. Code I used: ...
1
by: Matt Lowrance | last post by:
I have created an object that has a number of properties, that I am now trying to simply databind to a form. I am using the data designer to do it and just dragging and dropping the items onto the...
2
by: Luka Djigas | last post by:
Hello everyone, please, I need your help. I'm new to python, so I don't know if this will seem like a stupid question to some of you ... I have a need to write to a file (or just print on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.