Una funzione JavaScript getWeekIT() per ottenere il numero della settimana partendo da un oggetto di tipo data.
Si tratta di un miglioramento della precedente versione.

Il calcolo del numero della settimana, della data passata, viene fatto secondo le regole in uso in italia con il calendario italiano, ovvero:
  • il primo giorno della settimana è lunedì
  • la prima settimana dell'anno è quella che ha almeno 4 giorni

JavaScript: getWeekIT

/**
 * sgart.it
 * calcola la settimana della data passata usando il calendario italiano
 * considerando che il lunedì è il primo giorno della settimana
 * e che la prima settimana dell'anno deve avere almeno 4gg
 * es. (new Date()).getWeekIT()
 */
Date.prototype.getWeekIT = function () {
	// rimuovo l'orario
	var dateEnd = new Date(this.getFullYear(), this.getMonth(), this.getDate());
	var dayEnd = dateEnd.getDay()
	
	// trovo il lunedì della settimana passata (this)
	var offsetEnd = [-6,0,-1,-2,-3,-4,-5][dayEnd];
	dateEnd.setDate(dateEnd.getDate() + offsetEnd)
	if(dateEnd.getMonth()===11 && dateEnd.getDate()>= 29){
		// se il lunedì è dopo il 28 dicembre, 
		// si tratta della prima settimana dell'anno 
		return 1;
	}
	
	// calcolo il primo giorno dell'anno
	var dateBegin = new Date(dateEnd.getFullYear(), 0, 1);
	var dayBegin = dateBegin.getDay();
	
	// trovo il lunedì della prima settimana dell'anno
	var offsetBegin = [1, 0, -1, -2, -3, +3, +2][dayBegin];
	dateBegin.setDate(dateBegin.getDate() + offsetBegin)

	var diffTime = Math.abs(dateEnd - dateBegin);
	var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
	var weekNumber = Math.ceil(diffDays / 7);

	return weekNumber+1;
};
può essere richiamata con

JavaScript

var dt = new Date();
// numero della settimana
var week = dt.getWeekIT();
console.log(week);

Può essere testata con il codice seguente

JavaScript: Test

var dateTests = [
	{year: 2020, month: 0, day: 1, week: 1},
	{year: 2020, month: 0, day: 5, week: 1},
	{year: 2020, month: 0, day: 6, week: 2},
	{year: 2020, month: 0, day: 26, week: 4},
	{year: 2020, month: 0, day: 27, week: 5},
	{year: 2020, month: 0, day: 28, week: 5},
	{year: 2020, month: 0, day: 29, week: 5},
	{year: 2020, month: 0, day: 30, week: 5},
	{year: 2020, month: 0, day: 31, week: 5},
	{year: 2020, month: 11, day: 27, week: 52},
	{year: 2020, month: 11, day: 28, week: 53},
	{year: 2020, month: 11, day: 29, week: 53},
	{year: 2020, month: 11, day: 30, week: 53},
	{year: 2020, month: 11, day: 31, week: 53},

	{year: 2021, month: 0, day: 1, week: 53},
	{year: 2021, month: 0, day: 3, week: 53},
	{year: 2021, month: 0, day: 4, week: 1},
	{year: 2021, month: 11, day: 26, week: 51},
	{year: 2021, month: 11, day: 27, week: 52},
	{year: 2021, month: 11, day: 28, week: 52},
	{year: 2021, month: 11, day: 29, week: 52},
	{year: 2021, month: 11, day: 30, week: 52},
	{year: 2021, month: 11, day: 31, week: 52},

	{year: 2022, month: 0, day: 1, week: 52},
	{year: 2022, month: 0, day: 2, week: 52},
	{year: 2022, month: 0, day: 3, week: 1},
	{year: 2022, month: 11, day: 25, week: 51},
	{year: 2022, month: 11, day: 26, week: 52},
	{year: 2022, month: 11, day: 27, week: 52},
	{year: 2022, month: 11, day: 28, week: 52},
	{year: 2022, month: 11, day: 29, week: 52},
	{year: 2022, month: 11, day: 30, week: 52},
	{year: 2022, month: 11, day: 31, week: 52},

	{year: 2023, month: 0, day: 1, week: 52},
	{year: 2023, month: 0, day: 2, week: 1},
	{year: 2023, month: 11, day: 24, week: 51},
	{year: 2023, month: 11, day: 25, week: 52},
	{year: 2023, month: 11, day: 26, week: 52},
	{year: 2023, month: 11, day: 27, week: 52},
	{year: 2023, month: 11, day: 28, week: 52},
	{year: 2023, month: 11, day: 29, week: 52},
	{year: 2023, month: 11, day: 30, week: 52},
	{year: 2023, month: 11, day: 31, week: 52},

	{year: 2024, month: 0, day: 1, week: 1},
	{year: 2024, month: 0, day: 7, week: 1},
	{year: 2024, month: 0, day: 8, week: 2},
	{year: 2024, month: 1, day: 25, week: 8},
	{year: 2024, month: 1, day: 26, week: 9},
	{year: 2024, month: 1, day: 28, week: 9},
	{year: 2024, month: 1, day: 29, week: 9},
	{year: 2024, month: 11, day: 29, week: 52},
	{year: 2024, month: 11, day: 30, week: 1},
	{year: 2024, month: 11, day: 31, week: 1},

	{year: 2025, month: 0, day: 1, week: 1},
	{year: 2025, month: 0, day: 5, week: 1},
	{year: 2025, month: 0, day: 6, week: 2},
	{year: 2025, month: 11, day: 28, week: 52},
	{year: 2025, month: 11, day: 29, week: 1},
	{year: 2025, month: 11, day: 30, week: 1},
	{year: 2025, month: 11, day: 31, week: 1},

	{year: 2026, month: 0, day: 1, week: 1},
	{year: 2026, month: 0, day: 4, week: 1},
	{year: 2026, month: 0, day: 5, week: 2},
	{year: 2026, month: 11, day: 27, week: 52},
	{year: 2026, month: 11, day: 28, week: 53},
	{year: 2026, month: 11, day: 29, week: 53},
	{year: 2026, month: 11, day: 30, week: 53},
	{year: 2026, month: 11, day: 31, week: 53},

	{year: 2027, month: 0, day: 1, week: 53},
	{year: 2027, month: 0, day: 3, week: 53},
	{year: 2027, month: 0, day: 4, week: 1},
	{year: 2027, month: 11, day: 26, week: 51},
	{year: 2027, month: 11, day: 27, week: 52},
	{year: 2027, month: 11, day: 28, week: 52},
	{year: 2027, month: 11, day: 29, week: 52},
	{year: 2027, month: 11, day: 30, week: 52},
	{year: 2027, month: 11, day: 31, week: 52},

	{year: 2028, month: 0, day: 1, week: 52},
	{year: 2028, month: 0, day: 2, week: 52},
	{year: 2028, month: 0, day: 3, week: 1},
	{year: 2028, month: 11, day: 24, week: 51},
	{year: 2028, month: 11, day: 25, week: 52},
	{year: 2028, month: 11, day: 26, week: 52},
	{year: 2028, month: 11, day: 27, week: 52},
	{year: 2028, month: 11, day: 28, week: 52},
	{year: 2028, month: 11, day: 29, week: 52},
	{year: 2028, month: 11, day: 30, week: 52},
	{year: 2028, month: 11, day: 31, week: 52},

	{year: 2029, month: 0, day: 1, week: 1},
	{year: 2029, month: 0, day: 7, week: 1},
	{year: 2029, month: 0, day: 8, week: 2},
	{year: 2029, month: 11, day: 30, week: 52},
	{year: 2029, month: 11, day: 31, week: 1},

	{year: 2030, month: 0, day: 1, week: 1},
	{year: 2030, month: 0, day: 6, week: 1},
	{year: 2030, month: 0, day: 7, week: 2},
	{year: 2030, month: 11, day: 29, week: 52},
	{year: 2030, month: 11, day: 30, week: 1},
	{year: 2030, month: 11, day: 31, week: 1},

	{year: 2031, month: 0, day: 1, week: 1},
	{year: 2031, month: 11, day: 31, week: 1},

	{year: 2032, month: 0, day: 1, week: 1},
	{year: 2032, month: 11, day: 31, week: 53},

	{year: 2033, month: 0, day: 1, week: 53},	
	{year: 2033, month: 11, day: 31, week: 52},	

	{year: 2034, month: 0, day: 1, week: 52},
	{year: 2034, month: 11, day: 31, week: 52},

	{year: 2035, month: 0, day: 1, week: 1},
	{year: 2035, month: 11, day: 31, week: 1},
];

function testGetWeekIT(dates) {

	var failed = 0;
	for(var i = 0; i < dates.length; i++) {
		var item = dates[i];
		var dt = new Date(item.year, item.month, item.day);
		var w = dt.getWeekIT();
		var ok = w === item.week;
		if(ok===false){
			failed++;
		}
		var s = dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() 
			+ ', week: ' + w 
			+ ', week aspected: ' + item.week
			+ ', test: ' + ok;
		if(ok===true)
			console.log(s);
		else
			console.error(s);
	}
	if(failed > 0){
		console.error('Failed: ' + failed);
	}else{
		console.log('Test OK');
	}
}
// eseguo i test
testGetWeekIT(dateTests);
da un risultato simile a questo

Text

2020-1-1, week: 1, week aspected: 1, test: true
2020-1-5, week: 1, week aspected: 1, test: true
2020-1-6, week: 2, week aspected: 2, test: true
...
2032-12-31, week: 53, week aspected: 53, test: true
...
2032-12-31, week: 53, week aspected: 53, test: true
...
2034-12-31, week: 52, week aspected: 52, test: true
2035-1-1, week: 1, week aspected: 1, test: true
2035-12-31, week: 1, week aspected: 1, test: true
Test OK

Demo

Giorno: Mese: Anno:

Data:

Settimana:

Tags:
JavaScript184 Esempi225
Potrebbe interessarti anche: