Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding dates problem 1

Status
Not open for further replies.

sergeiY

Programmer
Feb 13, 2003
132
0
0
AU
Hi

I need to generate a list of dates between 2 dates for example:

1/7/2003
2/7/2003
3/7/2003
...
30/7/2003

it's trivial right ?

here is my code and I don't know why I am getting these results:

01/07/2003
7/2/2003
2/8/2003

Code:
<%
Option Explicit

Dim startDate, endDate, currDate, temp

startDate = &quot;01/07/2003&quot;
endDate = &quot;30/07/2003&quot;
%>
<table border=1>
<tr>
	<td>Stop Date</td>
</tr>
<%
currDate = startDate
do while DateDiff(&quot;d&quot;,currDate,endDate) > 0
%>
<tr>
  	<td><a><% Response.Write currDate%></a></td>
</tr>
<%
temp = DateAdd(&quot;d&quot;, 1, Day(currDate) & &quot;/&quot; & Month(currDate) & &quot;/&quot; & Year(currDate))
currDate = temp
loop %>
</table>

any ideas why it is doing it ? I am in Australia so my server date settings different to standard US format it's day first then month ...
 
personally i would have built this a little differently, but perhaps my alternate method might give you ideas, or help you figure out where the problem is in yours:

<%
'Reformat dates as necessary or use Session.LCID to you needs

StartDate = &quot;07/01/2003&quot;
EndDate = &quot;07/30/2003&quot;
StartDate = Cdate(StartDate)
EndDate = Cdate(EndDate)

StepValue = 1 ' modify this for partial day increments (0.5 for 12 hour increments, etc)

Do while TempDate < EndDate
TempDate = StartDate + Step
Response.Write TempDate & &quot;<br>&quot; & VbCrLf
Step = Step + StepValue
loop
%>
 
excelent ! Thank you this works NO problemos!
 
you're basic error is that you are using startDate and endDate as variables of subtype String - it's not really your fault: it is all too easy to do because VBscript doesn't force you to 'type' all Dims (that's a whole discussion by itself though!)

So - instead create date subtypes from your strings, then assign to those two variables. This has the added bonus of simplifying the rest of your VBscript: you can compare two Dims of subtype date using >= and you can directly add 1 day to currDate very easily.

Try this, works for me:
Code:
<%
Dim startDate, endDate, currDate

startDate = DateValue(&quot;01 Jul 2003&quot;)
endDate = DateValue(&quot;30 Jul 2003&quot;)
currDate = startDate
%>
<table border=1>
<tr>
    <td>Stop Date</td>
</tr>
<%

do while (currDate <= endDate)
	%>
<tr>
      <td><a><%=currDate%></a></td>
</tr>
	<%
	currDate =  DateAdd(&quot;d&quot;, 1, currDate)
loop 
%>
</table>

Note - when you use strings for dates always use something that isn't ambiguous .. in this case Jul will never be confused with a day ;).
Also - you could extend this example so that it got the first and last day of the current month (or a given month) automatically .. you wouldn't have to type it in. Could be useful if you need to use this for lots of different dates.

Good luck


Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
clarkin

your idea to use strngs for dates is really good I'll defenetly take it on board

Thank you

Sergei
 
DreXor,

What did you mean by:

'Reformat dates as necessary or use Session.LCID to you needs

What is Session.LCID ???
 
Session.LCID is a setting so that your currency, times, dates etc appear correctly for your location or other worldy locations as you deem necessary in a page
 
your idea to use strngs for dates is really good I'll defenetly take it on board

Just to clarify: I'm saying don't use strings to store your dates in VBscript - use the DATE subtype.
Code:
startDate = DateValue(&quot;01 Jul 2003&quot;)
'  ^DATE type             ^string

DateValue() creates a DATE from a string




Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
clarkin

You deserved a star for this one. I have to admit I had to face this issue for quite some time and never had an elegant solution for it. Well done!

Sergei
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top