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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Display month & last sunday of month. Help with my code

    Hello. I've been struggling with a bit of javascript. I need to display 'current month' and 'date of last Sunday in a month'

    Here's what I have so far. I'm getting closer. I can write the date of last sunday of the month but I can't seem to get the current month to display properly.

    Example: for this month I want the code to display January 27 but I'm getting December 27. 27 IS the correct date because 27 is the last Sunday in this month but (obviously) I need it to display January because Januuary IS the current month.


    Code:
    <script type="text/javascript">
    var d = new Date();
    var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];
    
    d.setMonth( d.getMonth() + 0 );
    d.setDate(0);
    lastsunday = d.getDate() - (d.getDay() + 3);
    
    document.write(monthNames[d.getMonth()]);
    document.write(" ");
    document.write(lastsunday);
    </script>
    If anyone can help me get that sorted I'd appreciate it.

  • #2
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post
    <!DOCTYPE html>
    <html>
    <body>

    <p id="demo">Click the button to display the name of this month.</p>

    <button onclick="myFunction()">Try it</button>

    <script>
    function myFunction()
    {
    var month=new Array();
    month[0]="January";
    month[1]="February";
    month[2]="March";
    month[3]="April";
    month[4]="May";
    month[5]="June";
    month[6]="July";
    month[7]="August";
    month[8]="September";
    month[9]="October";
    month[10]="November";
    month[11]="December";

    var d = new Date();
    var x = document.getElementById("demo");
    x.innerHTML=month[d.getMonth()];
    }
    </script>

    </body>
    </html>





    Hope this helps

  • Users who have thanked lucky7 for this post:

    pilsnermonkey (01-18-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Thumbs up

    Yes, that will do it. Thanks lucky. I was hoping to combine the two bits of code into one script. IE Display current month AND the date of the last Sunday (27) in the current month. Having it in one piece of code will not really be necessary. This will do just fine.

    Code:
    <script>
    document.write("<h1>Lucky is a rockstar!</h1>");
    </script>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    <script type="text/javascript">
    
    var monthNames = [ "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December" ];
    
    var today = new Date();  // or any specified date
    var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
    var dy = lastDayOfMonth.getDay();
    var dt = lastDayOfMonth.getDate();
    var date = dt-dy;
    var suffix = (date==1 || date==21 || date==31) ? "st" : "th" && (date==2 || date==22) ? "nd" : "th" && (date==3 || date==23) ? "rd" : "th";
    var result = monthNames[today.getMonth()] + " " +  date + suffix;
    alert ("The last Sunday of the month is " + result);
    
    </script>
    Quizmaster: What was the currency of Holland before the Euro?
    Contestant: Was it the Danish Krone?
    Last edited by Philip M; 01-18-2013 at 08:34 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    pilsnermonkey (01-19-2013)

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    There's only one thing really wrong with the original code:

    Quote Originally Posted by pilsnermonkey View Post
    Code:
    d.setDate(0);
    This sets the date to the last day of the previous month (why you were getting December instead of January) - to set to the last day of the current month you'd need to add 1 to the month as well.
    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:

    pilsnermonkey (01-19-2013)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by felgall View Post
    There's only one thing really wrong with the original code:



    This sets the date to the last day of the previous month (why you were getting December instead of January) - to set to the last day of the current month you'd need to add 1 to the month as well.
    In which case it needs to be

    Code:
    d.setMonth(d.getMonth() + 1);
    d.setDate(0);
    lastsunday = d.getDate() - (d.getDay());

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    pilsnermonkey (01-19-2013)

  • #7
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts
    <script>
    document.write("<h1>Phillip M is a Supreme Master coder and a rockstar!</h1>");
    </script>

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by pilsnermonkey View Post
    <script>
    document.write("<h1>Phillip M is a Supreme Master coder and a rockstar!</h1>");
    </script>
    Alas, document.write() is obsolete. And I am afraid that I am not a rock star. I have all the musical talent of Vincent van Gogh.
    I only know two tunes. One is "God Save The Queen", the other isn't.
    Last edited by Philip M; 01-19-2013 at 08:15 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    pilsnermonkey (01-19-2013)

  • #9
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Well if that God Save the Queen is Johnny & Sid's version I know that one too! Thanks for all your help. Got me out of a perplexing issue. document.write obsolete? Oh brother ... it's working for my purposes in this instance so I will not temp fate at this time. Cheers.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by pilsnermonkey View Post
    document.write obsolete? Oh brother ... it's working for my purposes in this instance so I will not temp fate at this time. Cheers.
    Netscape 3 is the most recent browser where there isn't a better way to interact with the web page from JavaScript than to use document.write so it has effectively been obsolete since that browser died 10+ years ago.

    With all current browsers you can use either innerHTML which resolves most of the problems that document.write had or you can use the many DOM commands that make up about 60% of JavaScript that even get around the few problems that still exist with innerHTML.

    No one should be being taught document.write any more as even browser dinosaurs such as Netscape 4 and IE4 had their own far better ways of interacting with the web page from JavaScript.

    document.write is history - not programming.
    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:

    pilsnermonkey (01-19-2013)

  • #11
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for clearing that up felgal. I'd better triple check browser compatibility ... and look into that more.

  • #12
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Resolved

    Resolved


  •  

    Posting Permissions

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