473,804 Members | 3,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create list of dates between two dates?

3 New Member
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 35804
bvdet
2,851 Recognized Expert Moderator Specialist
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
Charlote
3 New Member
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
Charlote
3 New Member
I solved it .. It look so simple now.. :)
Thx for the help..
Feb 26 '08 #4
bvdet
2,851 Recognized Expert Moderator Specialist
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
2145
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 newsgroup and others, I'm not the only one. Its just a poorly addressed issue within ASP. So for all your poor buggers out there that are having problems, particularly with european date formats, here is my solution. I have the user enter their...
0
1599
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 island. I have a table that holds the records when someone books a spot on the cruise. I have a query that counts how many bookings there are for a particular day subtracted from 24 so that I can see how many spots are open for that day. I also have...
5
3469
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 periods. What I need to do is take a given review date and find out which date in my pay periods table it's closest to, above or below. Any help is greatly appreciated. -Josh
18
6647
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 month, here is what I used: 1. Create a table named TblNumbers with one field named Num and populate the table with 1 to 31 2. Create a query based on TblNumbers 3. Pull down Num into the first field of the query. 4. Put the following...
1
4861
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
3298
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: NetUserEnum(ServerName, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume); if (EntriesRead > 0)
1
1385
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 form. Everything works great, except one of the items in my object is a list of dates. Right now I am using a generic list to hold these, but when I go to put this item on the form it only lists "None" as the type of control I can use to...
2
2255
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 screen, that part doesn't matter at this point) a list of dates, starting today. For example: 02.09.2008. tue 03.09.2008. wed et cetera
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10337
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9160
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.