Last active
September 28, 2022 22:14
-
-
Save HomerJSimpson/6093931 to your computer and use it in GitHub Desktop.
Calculate Second Monday of the month in javascript (not quite done)
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
/*jslint plusplus:true, white:true */ | |
/*global console:false */ | |
function secondMonday(year, month) { "use strict"; | |
var firstDay = new Date(year, month, 1); | |
if(firstDay.getDay() !== 1) { | |
firstDay.setDate( | |
firstDay.getDay() === 0 ? 2 : 9 - firstDay.getDay() | |
); | |
} | |
firstDay.setDate(firstDay.getDate() + 7); | |
return firstDay; | |
} | |
var year, month; | |
for(year=2017;year>2013;--year) { | |
for(month=0;month<12;++month) { | |
console.log(secondMonday(year, 11-month) + "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Had to do another calculation for last [weekday] of the month (e.g. last Sunday of August 2022) — as opposed to first, second, etc. Hat tip to @brucemcpherson for this snippet and to Dennis R. for this snippet: