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 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    158
    Thanks
    71
    Thanked 1 Time in 1 Post

    Displaying correct number of days in month

    Hi. I am working on a moble design that displays a list of years to the user. When they select a year, the months are shown. Finally, when they select a month, the days are shown. Its like my own custom calendar type thing. I do this by
    Code:
     
    var years = ["2013", "2014", "2015"];
    var months = ["Jan", "Feb", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
       showList2(years, function (year) {
          showList2(months, function (month) {
             showList2(days, function (day) {
                 $this.trigger("collapse");
                 updateDepartureDate2(year, month, day);
              });
           });
        });
    The showList method adds things to my list for display
    Code:
    function showList(items, onchoice) {
            var $this = $(this);
            var $list = $("#depart-container ul");
    
            var onItemTap = function () {
                var value = $(this).html();
    
                onchoice(value);
            }
    
            $list.html("");
    
            $.each(items, function (i, item) {
                var $li = $("<li/>").html(item).appendTo($list);
    
                $li.on("tap", onItemTap);
            });
        }
    With the above set up, how could I display the correct number of days for the chosen month? So if the user chooses february, the next list should only display 28 days. Not too sure where I can fit this in so any advice appreciated.

    Cheers

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    var dim = new Date(year, month, day) will give you a javascript date. If you enter 0 for the day, you will get the last day of the previous month (even in leap years.)
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    158
    Thanks
    71
    Thanked 1 Time in 1 Post
    Thanks, but I cant really use that approach with the setup I have. The list are created one at a time, so I cant do that date like this.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    Quote Originally Posted by nick2price View Post
    Thanks, but I cant really use that approach with the setup I have. The list are created one at a time, so I cant do that date like this.
    That one line of code substitutes for the several lines of code you'd need to get the number of days in a month any other way. There is no situation where it will not work to get the number of days in a month as long as you know the month.

    So doing the lists one at a time for 3 years you'd call the statement to get the number of days in the month 36 times.

    var daysinmonth = new Date(year, month, 0).getDate();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    nick2price (04-12-2013)

  • #5
    Regular Coder
    Join Date
    May 2009
    Posts
    158
    Thanks
    71
    Thanked 1 Time in 1 Post
    The problem is, I could look at doing something like
    Code:
    showList2(years, function (year) {
                showList2(months, function (month) {
                    var daysinmonth = new Date(year, month, 0).getDate();
                    showList2(daysinmonth, function (day) {
                        $this.trigger("collapse");
                        updateDepartureDate2(year, month, day);
                    });
                });
            });
    That assuming that month has a value such as febuary, which it doesnt at this stage. The value of the month comes from this line in the showList function
    Code:
    var value = $(this).html();
    So I cant do it this way as I dont have a value for month and year.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    But if you have a select for month and prepend the options with one that has a value="", and give all the rest a text of the month abbreviation, but a value of 1-12, submitting the value will ask for day 0 of one month later (or, the last day of that month.) You can also use the selectedIndex, because it will match the value.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    nick2price (04-12-2013)

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by nick2price View Post
    So I cant do it this way as I dont have a value for month and year.
    sure you do, thanks to the power of closure:

    Code:
    var years = ["2013", "2014", "2015"];
    var months = ["Jan", "Feb", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
       showList2(years, function (year) {
          showList2(months, function (month) {
           var daysinmonth = new Date(year, month, 0).getDate();
           var days2=days.slice(0, daysinmonth );
    
             showList2(days2, function (day) {
                 $this.trigger("collapse");
                 updateDepartureDate2(year, month, day);
              });
           });
        });
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    nick2price (04-12-2013)

  • #8
    Regular Coder
    Join Date
    May 2009
    Posts
    158
    Thanks
    71
    Thanked 1 Time in 1 Post
    Thanks for the reply. I have just realised that I need to do some work with the day also, so will need to try and get a working example using date. With the example you showed, my list for days is empty. I think this is because the showList function expects an array of numbers for the days. Is this what the slice does? When I try an alert on daysinmonth, it returns NaN.

    Thanks

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by nick2price View Post
    Thanks for the reply. I have just realised that I need to do some work with the day also, so will need to try and get a working example using date. With the example you showed, my list for days is empty. I think this is because the showList function expects an array of numbers for the days. Is this what the slice does? When I try an alert on daysinmonth, it returns NaN.

    Thanks
    well, NaN==Zero, so anArray.slice(0,0) returns an empty array, which sounds like what you see.

    so, we need to figure out why year or month isn't working.
    my guess?
    Date() is very picky about data types considering that js is loosely-typed. "3"!==3, and Date actually cares.
    Since you grab the html, you have a string. i could not be sure what was happening based upon the code in front of me, but this sounds like the issue.


    if my hunch is correct, this should fix it:

    Code:
    var years = ["2013", "2014", "2015"];
    var months = ["Jan", "Feb", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var days = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
       showList2(years, function (year) {
          showList2(months, function (month) {
           var daysinmonth = new Date( +year, +month, 0).getDate();
           var days2=days.slice(0, daysinmonth );
    
             showList2(days2, function (day) {
                 $this.trigger("collapse");
                 updateDepartureDate2(year, month, day);
              });
           });
        });
    blink and you'll miss it, the + number operators.
    you can also use the more-readable "*1", or even call the noob-friendly but very slow Number() function to get real numbers.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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