20. september 2006 - 22:03Der er
4 kommentarer og 1 løsning
Calendar og week
hej, jeg sidder og arbejder i visual studio 2005, og har en kalender. Jeg har sat kalenderen til at vise uger, men hvordan får jeg uge nummeret der står ud for den dag jeg trykker på?
Fandt selv ud af det, visual studio kan ikke så jeg fandt noget kode som kunne gøre arbejdet.
private void Kalender_ValueChanged(object sender, EventArgs e) { // Calculates the ISO 8601 Week Number // In this scenario the first day of the week is monday, // and the week rule states that: // [...] the first calendar week of a year is the one // that includes the first Thursday of that year and // [...] the last calendar week of a calendar year is // the week immediately preceding the first // calendar week of the next year. // The first week of the year may thus start in the // preceding year
const int JAN = 1; const int DEC = 12; const int LASTDAYOFDEC = 31; const int FIRSTDAYOFJAN = 1; const int THURSDAY = 4; bool ThursdayFlag = false;
// Get the day number since the beginning of the year int DayOfYear = Kalender.Value.DayOfYear;
// Get the numeric weekday of the first day of the // year (using sunday as FirstDay) int StartWeekDayOfYear = (int)(new DateTime(Kalender.Value.Year, JAN, FIRSTDAYOFJAN)).DayOfWeek; int EndWeekDayOfYear = (int)(new DateTime(Kalender.Value.Year, DEC, LASTDAYOFDEC)).DayOfWeek;
// Compensate for the fact that we are using monday // as the first day of the week if (StartWeekDayOfYear == 0) StartWeekDayOfYear = 7; if (EndWeekDayOfYear == 0) EndWeekDayOfYear = 7;
// Calculate the number of days in the first and last week int DaysInFirstWeek = 8 - (StartWeekDayOfYear); int DaysInLastWeek = 8 - (EndWeekDayOfYear);
// If the year either starts or ends on a thursday it will have a 53rd week if (StartWeekDayOfYear == THURSDAY || EndWeekDayOfYear == THURSDAY) ThursdayFlag = true;
// We begin by calculating the number of FULL weeks between the start of the year and // our date. The number is rounded up, so the smallest possible value is 0. int FullWeeks = (int)Math.Ceiling((DayOfYear - (DaysInFirstWeek)) / 7.0);
WeekNumber = FullWeeks;
// If the first week of the year has at least four days, then the actual week number for our date // can be incremented by one. if (DaysInFirstWeek >= THURSDAY) WeekNumber = WeekNumber + 1;
// If week number is larger than week 52 (and the year doesn't either start or end on a thursday) // then the correct week number is 1. if (WeekNumber > 52 && !ThursdayFlag) WeekNumber = 1;
tager den funktion datoen fra en calendar fra toolboxen og når man så trykker på en hvilken som helst dato i kalenderen, retunere den så den uge som den dag man har valgt indgår i?
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.