Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    Bunbury W.A
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Bunbury W.A
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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"

  • #4
    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
    why don't you just do the less then the first day of the next month?

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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"

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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
    I am the luckiest man in the world

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Bunbury W.A
    Posts
    157
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #8
    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
    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.
    Last edited by whammy; 11-08-2003 at 02:29 AM.
    Former ASP Forum Moderator - I'm back!

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


  •  

    Posting Permissions

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