Number of days in a month...my code not working

# Thread: Number of days in a month...my code not working

1. Senior Member
Join Date
Dec 1969
Posts
1,100

## Number of days in a month...my code not working

I have the code below which sholuld give me a medium day sales in a month (taking the total sales of a month and divide by the number of the day in a month)...<BR><BR>But it doesn&#039;t working as expected...<BR><BR>The function NumberOfDays give me the number of days 11 (not 30, or 31 or 28 - depending of the month).<BR><BR>And the total sales are divided at 11...<BR><BR>What can I do to find out the number of days in a month (depending of the month)?<BR><BR>Below is my code:<BR>------------<BR>CurrentYear=year(date)<BR>CurrentMonth_1 = month(date)-1<BR><BR>sql="SELECT sum(orderitems.price) as price FROM orders INNER JOIN orderitems ON orders.orderid = orderitems.orderid where month(date_order)="& CurrentMonth_1 &" and year(date_order) ="& CurrentYear &" and status=true "<BR><BR>set rs=con.execute(sql)<BR><BR>Function NumberOfDays(iMonth, iYear)<BR> NumberOfDays = Day(DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1)))<BR>End Function<BR><BR>medium_sales=rs_sales_medium("pric e")NumberOfDays(CurrentMonth_1,CurrentYear)<BR><BR >while not rs.eof<BR>if not rs("price")="" or not isnull(rs("price")) then<BR><BR><BR>%&#062;<BR>&#060;b&#062;&#060;%=fo rmatnumber(medium_sales)%&#062;&#060;/b&#062; euros<BR><BR>&#060;% <BR>else<BR>response.write "&#060;b&#062;no sales this month&#060;/b&#062;"<BR>end if<BR>------------<BR><BR>Thanks,<BR>Mircea

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Start by simplifying the function

For that matter, why bother with a function:<BR><BR>Function NumberOfDays(iMonth, iYear)<BR> NumberOfDays = DateSerial(iYear, iMonth + 1, 0)<BR>End Function<BR><BR>See? That&#039;s so simple you should just plug in<BR> DateSerial(iYear, iMonth + 1, 0)<BR>where you need it rather than bothering with writing a function.<BR><BR>*********<BR><BR>But this line is totally bogus:<BR> CurrentMonth_1 = month(date)-1<BR><BR>HUH??? WHY???<BR><BR>So if today were January 7th, you would get *ZERO* for CurrentMonth_1.<BR><BR>And then I double triple guarantee you that you would get *NO MATCHES* at all from your SQL statement, because <BR> Month(date_order) <BR>will *never* equal zero.<BR><BR>WHY are you doing the minus 1 ????<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
11,247

## No it gives you the right answer!

Function NumberOfDays(iMonth, iYear)<BR> NumberOfDays = Day(DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1)))<BR>End Function<BR>response.write "Numberof Days" & NumberOfDays(CurrentMonth_1,CurrentYear) & "<BR>"<BR><BR>Give me 30<BR>Which for November is correct!

4. Senior Member
Join Date
Dec 1969
Posts
11,247

## I agree his function is not correct

.

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## HUH?

The function is fine. It&#039;s the USE of it that makes no sense.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
11,247

## What did I say?

Oops not function<BR>Use &#060;humblepie eating/&#062;

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•