webForumDet fria alternativet

Kolla antalet veckor på ett år

JavaScript

5 svar · 516 visningar · startad av Erik Juhlin

Medlem sedan maj 200010 687 inlägg
Frågan#1

Hallå!

Finns det något sätt att i JavaScript räkna ut hur många veckor det finns ett visst år?

Medlem sedan juni 2003771 inlägg
#2

Förstår inte vitsen med det! Vad kan man uppnå med det?
Ett år består väl alltid av 53 veckor.
Men året börjar på vecka ett eller på vecka 53.

Vanligt år har 52.14 veckor
Skottår har 52,29 veckor

Medlem sedan sep. 20021 655 inlägg
#3

Utifrån reglerna beskrivna på http://www.hermetic.ch/cal_stud/palmen/lweek1.htm skrev jag följande:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>

<head>
<title>xxx</title>

<script type="text/javascript">
Date.prototype.weeksInYear = function(){
	var firstDay = new Date(this.getFullYear(), 0, 1),   // 1 januari
		lastDay  = new Date(this.getFullYear(), 11, 31); // 31 december

	var firstDayWeekDay = firstDay.getDay(), // Veckodag, där 0=sön, 1=mån, 2=tis, o.s.v.
		lastDayWeekDay  = lastDay.getDay();

	return (firstDayWeekDay == 4 || lastDayWeekDay == 4) ? 53 : 52;
}

function weeksInYear(yearNum) {
	var firstDay = new Date(yearNum, 0, 1),   // 1 januari
		lastDay  = new Date(yearNum, 11, 31); // 31 december

	var firstDayWeekDay = firstDay.getDay(), // Veckodag, där 0=sön, 1=mån, 2=tis, o.s.v.
		lastDayWeekDay  = lastDay.getDay();

	return (firstDayWeekDay == 4 || lastDayWeekDay == 4) ? 53 : 52;
}

/*** Två olika sätt att hitta antalet veckor i ett år ***/
var weeksInYear2005 = (new Date(2005,0,1)).weeksInYear();
var weeksInYear2006 = weeksInYear(2006);
</script>

</head>

<body>

<script type="text/javascript">
document.write("<table>");
var year = 1970;
while(year < 2038) {
	document.write("<tr><td>" + year + "</td><td>" + weeksInYear(year) + "</td></tr>");
	year++;
}
document.write("</table>");
</script>

</body>

</html>

Den uppritade tabellen är bara för att verifiera att det stämmer - om du har någon sådan tabell tillgänglig är det bara att jämföra.

Medlem sedan juni 2003771 inlägg
#4

Då undrar jag hur ett år kan ha bara 52 veckor!

Ett är består av 365 dagar, delar vi det med veckor (alltså 7)
så får vi 365 / 7 = 52,143. För skottår blir det 52,296

Då får inte dagarna plats inom 52 veckor.
Däremot kan det finnas 52 hela veckor, samt en bruten.

Stämmer inte detta påstående?

Medlem sedan sep. 20021 655 inlägg
#5

Man talar om hur många veckor som tillhör året.
http://www.cl.cam.ac.uk/~mgk25/iso-time.html
Köp gärna http://www.iso.org/iso/en/CatalogueDetailPage.CatalogueDetail?CSNUMBER=40874&ICS1=1&ICS2=140&ICS3=30 om du vill ha mer kött på benen.

Medlem sedan jan. 20016 874 inlägg
#6

OT, dock koolt ;)

Note: The ISO standard avoids explicitly stating the possible range of week numbers, but this can easily be deduced from the definition. Possible ISO week numbers are in the range 01 to 53. A year always has a week 52. (There is one historic exception: the year in which the Gregorian calendar was introduced had less than 365 days and less than 52 weeks.)
Proof: Per definition, the first week of a year is W01 and consequently days before week W01 belong to the previous year and so there is no week with lower numbers. Considering the highest possible week number, the worst case is a leap year like 1976 that starts with a Thursday, because this keeps the highest possible number of days of W01 in the previous year, i.e. 3 days. In this case, the Sunday of W52 of the worst case year is day number 4+51*7=361 and 361-366=5 days of W53 belong still to this year, which guarantees that in the worst case year day 4 (Thursday) of W53 is not yet in the next year, so a week number 53 is possible. For example, the 53 weeks of the worst case year 1976 started with 1975-12-29 = 1976-W01-1 and ended with 1977-01-02 = 1976-W53-7. On the other hand, considering the lowest number of the last week of a year, the worst case is a non-leap year like 1999 that starts with a Friday, which ensures that the first three days of the year belong to the last week of the previous year. In this case, the Sunday of week 52 would be day number 3+52*7=367, i.e. only the last 367-365=2 days of the W52 reach into the next year and consequently, even a worst case year like 1999 has a week W52 including the days 1999-12-27 to 2000-01-02. q.e.d.

276 ms totalt · 4 externa anrop · v20260731065814-full.a51de22e
128 ms — deklarationer (db)
0 ms — hämta statistik (cache)
141 ms — hämta tråd, inlägg och bilagor (db)
131 ms — ändringar (db)