Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2003, 10:24 PM   PM User | #1
dawilis
Regular Coder

 
Join Date: Jul 2002
Location: Bunbury W.A
Posts: 157
Thanks: 0
Thanked 0 Times in 0 Posts
dawilis is an unknown quantity at this point
geting last day of any month

Im writing an sql statement that gets any records between the 1st of a month and the last day of a month
how can I determine the last day of any month dynamically and not necesarily the current month
dawilis is offline   Reply With Quote
Old 11-04-2003, 11:28 PM   PM User | #2
dawilis
Regular Coder

 
Join Date: Jul 2002
Location: Bunbury W.A
Posts: 157
Thanks: 0
Thanked 0 Times in 0 Posts
dawilis is an unknown quantity at this point
Found something

I found this bit of javascript but cant get it to run any suggestions

enddays = getDays(2,2003)

<script language="JavaScript">
function getDays(month, year) {
// create array to hold number of days in each month
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December

// return number of days in the specified month (parameter)
return ar[month]
}
function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
/* else */ // else not needed when statement is "return"
return false // is not leap year
}
</script>
dawilis is offline   Reply With Quote
Old 11-06-2003, 01:12 AM   PM User | #3
M@rco
Regular Coder

 
Join Date: Oct 2003
Location: London, UK
Posts: 411
Thanks: 0
Thanked 1 Time in 1 Post
M@rco is an unknown quantity at this point
My suggestion? Scrap the script and write your own! It's pretty poor, and was clearly written by a pretty bad coder... here's why:

- the array "ar" is dimensioned with 13 items (0-12), but only 12 are used.

- getDays() accepts a month between 0 and 11 (inclusive), not 1-12 like everything else on the planet.

- the leap year function does not perform sufficient checks to correctly determine whether it really IS a leap year or not. It's code like this that caused the Y2K problem... see here for more, including how leap years ARE calculated.

- there is no error checking (so months <0 or >11 or non-numeric will fail, and silly years will also fail)


I could go on, but those are the worst flaws. I'm tempted to write my own function and present it here, but then that wouldn't help you learn, and the fact that you are cutting & pasting a script suggests that perhaps you should.

The information above (including the link) should be enough to allow you to develop your own script, and it's a excellent little problem to use as a learning experience so give it a go! If you get stuck, post what you've got so far...
__________________
Marcus Tucker / www / blog
Web Analyst Programmer / Voted SPF "ASP Guru"
M@rco is offline   Reply With Quote
Old 11-06-2003, 02:42 AM   PM User | #4
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
why don't you just do the less then the first day of the next month?
A1ien51 is offline   Reply With Quote
Old 11-06-2003, 02:53 AM   PM User | #5
M@rco
Regular Coder

 
Join Date: Oct 2003
Location: London, UK
Posts: 411
Thanks: 0
Thanked 1 Time in 1 Post
M@rco is an unknown quantity at this point
That approach would work perfectly well in VBScript with the DateDiff() function, but AFAIK JavaScript has no equivalent, so it can't be done without writing your own function (which would in turn require the use of a function precisely like the one under discussion)!!! lol
__________________
Marcus Tucker / www / blog
Web Analyst Programmer / Voted SPF "ASP Guru"
M@rco is offline   Reply With Quote
Old 11-06-2003, 09:47 AM   PM User | #6
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
Code:
function getlastdateofmonth (month, year) {
  var last = new Date(new Date(year, month + 1, 1).valueOf() - 1).getDate();
  return last;
}
this should do the trick

months are in the range 0 - 11, larger or higher values dont result in error messages because of the internal translation of the date objectconstructor

the function creates a date object of the first day of the next month, then it converts it to milliseconds passed since 1-1-1970 00:00:00, subtract 1 millisecond to get the last millisecond of the previous day (this is the last day of the month you need), and get the date from that day
Roelf is offline   Reply With Quote
Old 11-06-2003, 12:21 PM   PM User | #7
dawilis
Regular Coder

 
Join Date: Jul 2002
Location: Bunbury W.A
Posts: 157
Thanks: 0
Thanked 0 Times in 0 Posts
dawilis is an unknown quantity at this point
Thanks reolf

Reolf
Thats great I can see here what you have done and have learned heaps, Having the code in front of me and then a little explanation sure helps to understand it, I appreciate all the other Helpfull comments also
Daz
dawilis is offline   Reply With Quote
Old 11-08-2003, 02:23 AM   PM User | #8
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
Very nice piece of code there, Roelf (I think, I haven't had a chance to test it extensively but it looks good!).

Just one thing, you can do it in one line without declaring a variable at all:
Code:
function getLastDateOfMonth(month, year)
{
  return new Date(new Date(year, month + 1, 1).valueOf() - 1).getDate();
}
However, I'd probably do some error checking with regular expressions beforehand to make sure that the parameters passed to the function are valid - that is if anyone else might be using this script.
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)

Last edited by whammy; 11-08-2003 at 02:29 AM..
whammy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:03 PM.


Advertisement
Log in to turn off these ads.