Skip to content

Instantly share code, notes, and snippets.

@dillten
Created January 14, 2015 21:45
Show Gist options
  • Save dillten/d0bb1a90bbddab95499d to your computer and use it in GitHub Desktop.
Save dillten/d0bb1a90bbddab95499d to your computer and use it in GitHub Desktop.
Get the second Monday of the month using moment.js
var mo = require('moment');
function getSecondMonday(startDate) {
var startOfMonth = mo(startDate).utc().startOf('month').startOf('isoweek');
var studyDate = mo(startDate).utc().startOf('month').startOf('isoweek').add(2, 'w');
if (studyDate.month() == startOfMonth.month()) {
studyDate = studyDate.subtract(1, 'w');
}
return studyDate;
}
for(var i = 0; i < 20; i++) {
var startOfMonth = mo().utc().subtract(i, 'M');
console.log(getSecondMonday(startOfMonth).toISOString());
}
@ashwin-chandrappa
Copy link

can this be made generic ?
like get date of nth weekday ??

like get date of 1st Sunday of month

@a-vi-nash
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment