Define the third Friday of the month of the month and year
Given the year and month, I would like to determine the date for the third Friday of this month. How can I use moment.js to define this?
eg. October 2015 => 16th October 2015
+1
Helen che
source
to share
1 answer
Given the year and month as integers, and assuming Friday is the fifth day of the week in your region (Monday is the first day of the week), you can:
function getThirdFriday(year, month){
// Convert date to moment (month 0-11)
var myMonth = moment({year: year, month: month});
// Get first Friday of the first week of the month
var firstFriday = myMonth.weekday(4);
var nWeeks = 2;
// Check if first Friday is in the given month
if( firstFriday.month() != month ){
nWeeks++;
}
// Return 3rd Friday of the month formatted (custom format)
return firstFriday.add(nWeeks, 'weeks').format("DD MMMM YYYY");
}
If you have the month and year as a string, you can use the moment parsing functions instead of naming an object, so you would have:
var myMonth = moment("October 2015", "MMMM yyyy");
If Friday is not the fifth day of the week (day with index 4), you can get the correct index using moment.weekdays()
+2
VincenzoC
source
to share