View Full Version : geting last day of any month

11-04-2003, 11:24 PM
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

11-05-2003, 12:28 AM
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

11-06-2003, 02:12 AM
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 (http://www.mitre.org/tech/y2k/docs/PROB.html#Leap) 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...

11-06-2003, 03:42 AM
why don't you just do the less then the first day of the next month?

11-06-2003, 03:53 AM
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

11-06-2003, 10:47 AM
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

11-06-2003, 01:21 PM
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

11-08-2003, 03:23 AM
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:

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.