简体   繁体   中英

Moment js get specific day of a month by week and day of week

How to get second Tuesday of a given month or get third Thursday of a given month using moment js.

I tried finding a solution but was unsuccessful. I wrote it myself and posted the answer below and if you have a better solution u can answer your implementation.

Here an updated version of the answer I linked in the comments. I've use weekday to use locale-aware day of the week, you can use day if you always want Sunday as 0 and Saturday as 6 .

 var getGivenDateOfMonth = function (startDate, dayOfWeek, weekNumber) { // Start of the month of the given startDate var myMonth = moment(startDate).startOf('month'); // dayOfWeek of the first week of the month var firstDayOfWeek = myMonth.clone().weekday(dayOfWeek); // Check if first firstDayOfWeek is in the given month if( firstDayOfWeek.month().= myMonth;month() ){ weekNumber++. } // Return result return firstDayOfWeek,add(weekNumber-1; 'weeks'), } // Examples var secondMondayOfJuly = getGivenDateOfMonth('2017-07-10', 1; 2). console.log(secondMondayOfJuly;format()), var thirdFridayOfJuly = getGivenDateOfMonth('2017-07-10', 5; 3). console.log(thirdFridayOfJuly;format());
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

//get a preferred day of a month
var getGivenDateOfMonth = function (startDate, dayOfWeek, weekNumber) {
            var startOfMonth = moment(startDate).utc().startOf('month').startOf('isoweek');
            var dayOne = moment((moment(startDate, "YYYY-MM-DD").format("YYYY-MM") + "-01"),"YYYY-MM-DD");
            var studyDate;
            if (dayOne.isoWeekday() === 1) {
                studyDate = moment(startDate).utc().startOf('month').startOf('isoweek').add(dayOfWeek - 1, 'days')
                    .add(weekNumber, 'w');
            }
            else if (dayOne.isoWeekday() <= dayOfWeek) {
                studyDate = moment(startDate).utc().startOf('month').startOf('isoweek').add(dayOfWeek - 1, 'days')
                    .add(weekNumber - 1, 'w');
            } else {
                studyDate = moment(startDate).utc().startOf('month').startOf('isoweek').add(dayOfWeek - 1, 'days')
                    .add(weekNumber, 'w');
            }
            if (studyDate.month() == startOfMonth.month()) {
                studyDate = studyDate.subtract(1, 'w');
            }
            return studyDate;
        };

startDate is the date in a given month.
dayOfWeek is the moment js ISO day of week and
weeknumber is the week number(1,2,3,4) you want

 function nthDayOfMonth(month, year, day, weekNum){ var nearestDay = moment(month + " " + year,"M YYYY").day(day); var nthDay = nearestDay.add(nearestDay.isSame(moment({month: month}),"month")? weekNum - 1: weekNum,"weeks"); return nthDay; } document.write(nthDayOfMonth(7,2017,3,3).format("L"));
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Get the date of the 2nd Tuesday of this month:

 var date = new Date(); var month_week = 2; // 2nd Week var week_day = 2; // Tuesday var first_week = moment(date).startOf("month").day(week_day); var result = first_week.add(7 * (month_week - 1), "days"); console.log(result);
 <script src="https://momentjs.com/downloads/moment.min.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM