Print Page | Close Window

Months between two dates

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=9535
Printed Date: 31 March 2026 at 11:22pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Months between two dates
Posted By: Maxi
Subject: Months between two dates
Date Posted: 04 February 2004 at 11:15am

Hi there,

I am busy making a little events calendar for my website where I allow users to submit events.
At the moment I have the months of the year set as categories, so if the event occurs in January the user will choose the January category:
However I can see that this is hardly effective when an event occurs for example over 4 months (eg. 01/01/04 - 01/04/04), as if they press the category February the event will not show up because it is in The january Category.

MY question is then, how can I get the month numbers between two Dates so that I can use them in a statement (eg. Jan-April has month numbers 1,2,3,4). to call all Events for a particular month.

Hope somebody can help out here.

Kind Regards

Maxi




Replies:
Posted By: zaboss
Date Posted: 04 February 2004 at 12:14pm

Well, the best thing is to have 2 fields in the db: StartingDate & EndingDate and the use appropriate select queries to trigger the right result.

According to your needs you should use an appropriate Date Function (dateAdd or datePart). More about asp functions http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vbscripttoc.asp - here .

Sorry I have putted a wrong url in the first place.



-------------
Cristian Banu
http://www.soft4web.ro - Soft 4 web


Posted By: Maxi
Date Posted: 04 February 2004 at 2:08pm

Hi zaboss,

Thanks for your quick reply.

I already have two date fields in my database (start and end date).

What I am wanting to do is Ad a function on the Form, where the user submits the Event, that will give me the months between two Dates.

Then I would like to Take the result of this calculation and have it on the Form as a Hidden Field which gets posted to a new field in my database.

Once I have these fields in my Database it will be easy to make the selection for months( Example WHERE months=1)

I am not a star programer and was hoping that somebody could tell me if what I want done above can be done, and where to find the proper means of adding this function to my page.

Thanks for the microsoft VB link, but being an amateur I was hoping for some kind of Tutorial or link that explains to a "Dummy Like Me" how to go about making this feature;)

Kind Regards and thanks again

Maxi



Posted By: Maxi
Date Posted: 04 February 2004 at 3:20pm

Question refraced,

Sorry to write so soon again but I would like to refrace the question that I have asked, as it might make what I am trying to do more clear.

The first question is:
Is it at all possible to get the name of the months between two dates?
The reason that I ask is because every article I am reading about Datediff seems to only be able to produce the number of months between two dates and not the month names (Or month numbers).

The second question is:
If it is possible would I be able to add the month (Multiple months) name to one field in the database, from which I could direct my queries at?

Kind regards, and I hope that my need are clearer now;)

Maxi

 

 



Posted By: zaboss
Date Posted: 05 February 2004 at 4:02am
Basically, what you want is that if an event starts 12 january and ends 14 april to enter in the db january, february, march, april? Isn't it?

-------------
Cristian Banu
http://www.soft4web.ro - Soft 4 web


Posted By: Maxi
Date Posted: 05 February 2004 at 5:22am

Hi Zaboss,

Thanks for replying,

Yes that is exactly what I would like to do. It would however be best if those months could be written as there VBSCRIPT month number (As it would be easy to call the month by its number).

Do you have any suggestions on how to do this, bare in mind that I am still an amateur;)

Kind Regards
Maxi  



Posted By: zaboss
Date Posted: 05 February 2004 at 5:42am

Not sure if this is the best way of doing this, but here it is what I'll do (at least, until I find a better way).

StrLast = DateDiff("m", Request.Form("EndDate"), Request.Form("StartDate"))
StrStart =DatePart("m", Request.Form("StartDate")
StrEnd = DatePart("m", Request.Form("EndDate")

For i = StrStart to StrEnd
Insert Into... ' insert into the db the values you get
If i > StrEnd then Exit for
next

Something like that...



-------------
Cristian Banu
http://www.soft4web.ro - Soft 4 web


Posted By: Maxi
Date Posted: 05 February 2004 at 7:40am

Hi zaboss

Is it possible for you to make the above demo with dates so that I can run it on a test page and see the results? How van I call the results to be displayed on my page?

I have added dates like this below, and now I would like to see the results before I start trying to figure out how to put the results into a hidden field on my form;)

<%
StrLast = DateDiff("m", 15-10-2003, 15-07-2003)
StrStart =DatePart("m", 15-07-2003)
StrEnd = DatePart("m", 15-10-2003)

For i = StrStart to StrEnd
If i > StrEnd then Exit for
next
%>

Thanks in advance

Regards

Maxi

 



Posted By: zaboss
Date Posted: 05 February 2004 at 9:45am
There isn much to it...just grab the values of i and instert them into db. That's all.

-------------
Cristian Banu
http://www.soft4web.ro - Soft 4 web


Posted By: Maxi
Date Posted: 05 February 2004 at 10:13am

Hi zaboss

Thanks for trying to help;). Being an amateur I am not sure how to just Grab the values of i and insert them into the DB or Field.

As I wrote in my previous mail, I would have liked to see what results the Function turned out first (make sure that it does it correctly), before I started trying to feed that result to a form field or database.

Kind Regards and thanks again 

maxi



Posted By: zaboss
Date Posted: 05 February 2004 at 10:23am

StrLast = DateDiff("m", Request.Form("EndDate"), Request.Form("StartDate"))
StrStart =DatePart("m", Request.Form("StartDate")
StrEnd = DatePart("m", Request.Form("EndDate")

For i = StrStart to StrEnd
Response.Write ""& i &""

If i > StrEnd then Exit for
next

Just add the line in red. Not sure about the number of doublequottes, though... Maybe should be only 1 pair... Sorry, don't have the time right know.



-------------
Cristian Banu
http://www.soft4web.ro - Soft 4 web


Posted By: Maxi
Date Posted: 05 February 2004 at 11:00am

Hi Zaboss,

I tried your code on a test page and added some dates to see what results came out of it.
Either I am doing something seriously wrong or there is a problem with this script? The answer that was returned to me was 7? I do not know where the 7 comes from as the answer should be: 8,9,10?
Here is the code that I had on the test page.

<%
StrLast = DateDiff("m", 15-10-2003, 15-08-2003)
StrStart = DatePart("m", 15-08-2003)
StrEnd = DatePart("m", 15-10-2003)

For i = StrStart to StrEnd
Response.Write ""& i &""
If i > StrEnd then Exit for
Next
%>

Regards

Maxi:)



Posted By: zaboss
Date Posted: 05 February 2004 at 11:34am

It seems that there is something wrong with the DatePart function's output... If I changed the date to:

<%
StrLast = DateDiff("m", 15-10-2003, 15-08-2003)
StrStart = DatePart("m", date)
StrEnd = DatePart("m", 10/12/2004)
Response.Write strStart
Response.Write "<br>" & strEnd
Response.Write "<br>"
i = strStart
For i = StrStart to StrEnd
Response.Write ""& i &"" & "; "
If i > StrEnd then Exit for
Next
%>

This is outputing 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12;

Which is then easy to insert into db and then grab back. Notice that I have added a small piece for better diferentiate records. I'm afraid I have to leave you with this as here it is already late and my lady is already mad at me. So, you should check what's wrong with the output of datePart function. After you figure it up, just write an instert into db statement for i.



-------------
Cristian Banu
http://www.soft4web.ro - Soft 4 web



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net