JavaScript
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, unused:true, curly:false, browser:true, jquery:true, indent:4, maxerr:50, laxcomma: true */
$(function() {
'use strict';
var expectations
, ta = $('textarea')
, result = ''
, i
, e
, date
, ewom
, wom
, tab = '\t'
, newline = '\r\n'
;
Date.prototype.getWeekOfMonth = function(exact) {
var month = this.getMonth()
, year = this.getFullYear()
, firstWeekday = new Date(year, month, 1).getDay()
, lastDateOfMonth = new Date(year, month + 1, 0).getDate()
, offsetDate = this.getDate() + firstWeekday - 1
, index = 1 // start index at 0 or 1, your choice
, weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)
, week = index + Math.floor(offsetDate / 7)
;
if (exact || week < 2 + index) return week;
return week === weeksInMonth ? index + 5 : week;
};
function expect(date, ewom, wom) {
return { date: date, exactWeekOfMonth: ewom, weekOfMonth: wom };
}
expectations = [
expect('2018-12-03', 1, 1)
, expect('2013-02-05', 2, 2)
, expect('2013-02-14', 3, 3)
, expect('2013-02-23', 4, 4)
, expect('2013-02-24', 5, 6)
, expect('2013-02-28', 5, 6)
, expect('2013-03-01', 1, 1)
, expect('2013-03-02', 1, 1)
, expect('2013-03-03', 2, 2)
, expect('2013-03-15', 3, 3)
, expect('2013-03-17', 4, 4)
, expect('2013-03-23', 4, 4)
, expect('2013-03-24', 5, 5)
, expect('2013-03-30', 5, 5)
, expect('2013-03-31', 6, 6)
];
result += 'Date';
result += tab;
result += ' ? ';
result += tab;
result += 'EWOM';
result += tab;
result += 'WOM';
result += newline;
for (i = 0; i < expectations.length; i++)...