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

    Javascript function to determine nth day for today

    i need a javascript function to return what occurrence of a day it is in a month.
    for example today is wednesday so if I pass in today's date to the function, it returns 4 as its 4th wednesday of the month

    Basically, I need opposite of this function:

    Code:
    function getNthDayOfMonth(index, day, month, year) {
        // Create date object
        var date = new Date();
        // Set to first day of month
        date.setDate(1);
        // If supplied - set the month
        if (month !== '' && month !== undefined) {
            // Set month
            date.setMonth(month);
        } else {
            month = date.getMonth();
        }
        // If supplied - set the year
        if (year !== '' && year !== undefined) {
            // Set year
            date.setFullYear(year);
        } else {
            year = date.getFullYear();
        }
        // Find daynumber
        firstDay = date.getDay();
        // Find first friday.
        while (date.getDay() != day) {
            date.setDate(date.getDate() + 1);
        }
        switch(index) {
            case 2:
                date.setDate(date.getDate() + 7);
                break;
            case 3:
                date.setDate(date.getDate() + 14);
                break;
            case 4:
                date.setDate(date.getDate() + 21);
                break;
            case 5:
                date.setDate(date.getDate() + 28);
                if (date.getMonth() !== month) {
                    date = null;
                }
                break;
        }
        // date = (date.getMonth(date)+1) + "/" + date.getDate(date) + "/" + date.getFullYear(date);
        return date;
    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    Pardon me, but why not just use a tiny bit of brute force?
    Code:
    <script type="text/javascript">
    function whichDayOccurrence( theDate )
    {
        var occurrence = 0;
        for ( var d = 1; d <= theDate.getDate(); ++d )
        {
            if ( (new Date( theDate.getFullYear(), theDate.getMonth(), d ) ).getDay() 
                 == theDate.getDay() 
            ) { ++occurrence; }
        }
        return occurrence;
    }
    
    alert( whichDayOccurrence( new Date() ) );
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    dknight (06-27-2013)

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

    Thumbs up

    Quote Originally Posted by Old Pedant View Post
    Pardon me, but why not just use a tiny bit of brute force?
    Code:
    <script type="text/javascript">
    function whichDayOccurrence( theDate )
    {
        var occurrence = 0;
        for ( var d = 1; d <= theDate.getDate(); ++d )
        {
            if ( (new Date( theDate.getFullYear(), theDate.getMonth(), d ) ).getDay() 
                 == theDate.getDay() 
            ) { ++occurrence; }
        }
        return occurrence;
    }
    
    alert( whichDayOccurrence( new Date() ) );
    </script>
    Thanks for that. This is less of a brute force that what I was thinking. I was trying to extract the month from a given date, then count number of days in that month and then see where that day lies in the month.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    And your getNthDayOfMonth can be written a lot more simply:

    Code:
    function getNthDayOfMonth(index, day, month, year) {
        var today = new Date();
        month = month || today.getMonth();
        var dt;
        for ( var d = 1; d <= 7; ++d )
        {
            var dt = new Date( year || today.getFullYear(), month, d );
            if ( dt.getDay() === day ) break;
        }
        dt.setDate( d + 7 * (index-1) );
        if ( dt.getMonth() !== month ) dt = null;
        return dt;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Can't you just divide the date integer by 7?

    Code:
    function nthWeekdayOfMonth(date){
    	if(date== undefined) date= new Date();
    	return Math.ceil(date.getDate()/7);
    }

  • Users who have thanked mrhoo for this post:

    dknight (06-27-2013)

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    LOL! YES!

    I didn't think of using Math.ceil()!

    Tch tch and triple tch on me.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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