Created
September 9, 2013 16:50
-
-
Save abitgone/6498356 to your computer and use it in GitHub Desktop.
This uses moment.js to select the date of the next Leamington Geeks meetup. It chooses either the 3rd Tuesday of the month if it's December or the last Tuesday of the month if it's any other month. If that date has passed, it adds a month and starts the process again. Is this the best way to do that?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var today = moment(new Date()), | |
monthEnd, | |
nextDate; | |
do { | |
if (monthEnd === undefined) { | |
monthEnd = moment(new Date()); | |
} | |
else { | |
monthEnd = monthEnd.add('months', 1); | |
} | |
if (monthEnd.month() == 11) { | |
// If it's December, choose the 3rd Tuesday | |
nextDate = monthEnd.startOf('month').startOf('week').add('days', 2).add('weeks', 2); | |
} | |
else { | |
// Choose the last Tuesday | |
nextDate = monthEnd.EndOf('month').subtract('days', Math.abs((2 - monthEnd.weekday() - 7) % 7)); | |
} | |
} while (nextDate.isBefore(today)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment