Peter,

I'm not sure `Date` class in EScript have Day DayOfWeek properties.

but based on your requiement I did this, a little augly but works:


function dayInMonth(date, weekday, n) {
    var day = date.getDay();

    if (day !== weekday) {
        return false;
    }

    var preMonthLast = new Date(new Date().setTime(date.getTime() - n * 7 * 
86400000));
    var first = new Date(new Date().setTime(date.getTime() - (n - 1) * 7 * 
86400000));
    if (date.getMonth() - preMonthLast.getMonth() === 1 ||
        (date.getMonth() === 0 && preMonthLast.getMonth() === 11)) {
        if (date.getMonth() === first.getMonth()) {
            return true;
        }
    }

    return false;
}

console.log(dayInMonth(new Date('2016-12-01T00:00:01'), 4, 1));
console.log(dayInMonth(new Date('2016-12-02T00:00:01'), 5, 1));
console.log(dayInMonth(new Date('2016-12-08T00:00:01'), 4, 2));

console.log(dayInMonth(new Date('2017-01-03T00:00:01'), 2, 1));
console.log(dayInMonth(new Date('2017-01-03T00:00:01'), 2, 2));




On Sunday, November 6, 2016 at 1:31:22 PM UTC+8, Peter Scargill wrote:
>
> Can someone give me a hand...
>
> I need a simple function, given a day of the week - and a number, to do 
> queries like "is this the 3rd Monday of the month" - returning true or 
> false.
>
> I thought I had it here but it is returning false regardless. Can anyone 
> spot what's wrong?
>
> function dayinmonth(weekday,n)
> {
>     var date=new Date();
>         if (n > 0)
>         {
>             var first = new Date(date.Year, date.Month, 1);
>             return ((date.Day - first.Day)/ 7 == n - 1) && (date.DayOfWeek 
> == weekday);
>
>         }
>         else
>         {
>             var last = new Date(date.Year, date.Month, 
> 1).AddMonths(1).AddDays(-1);
>             return ((last.Day - date.Day) / 7 == (Math.Abs(n) - 1)) && 
> (date.DayOfWeek == weekday);
>         }  
> }
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/011eb4c7-7694-4713-b56f-f76f6293429e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to