Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week of year berechnen #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/extensions/date_time.extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,32 @@ extension DateTimeExtension on DateTime {
bool get isSameYear {
return DateTime.now().year == year;
}

int get weekOfYear {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brauchen denke ich einen anderen Namen dafür. Wusste nicht direkt was damit gemeint ist

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weekOfTheYear oder andere vorschläge?

Copy link
Member

@JPM84 JPM84 Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.weekOfYear passt eigentlich, ist typisch z.B. in C#.

.week oder .day wäre noch schöner.

// Datum auf Mitternacht setzen
DateTime date = DateTime(year, month, day);

// Berechne die differenz zum Beginn des Jahres
int dayOfYear = date.difference(DateTime(year, 1, 1)).inDays + 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Würde zwei extensions hieraus machen.
.dayOfYear
.weekOfYear

Ein Test wäre hier zwingend für einen Merge.


// Wochendtag des 1. Januar (1 = Montag, 7 = Sonntag).
int weekdayOfFirstDay = DateTime(year, 1, 1).weekday;

// Korrigiere zum ISO 8601-Format (Montag = 0, Sonntag = 6).
weekdayOfFirstDay--;

// Berechne die ISO-Woche.
int weekNumber = ((dayOfYear - weekdayOfFirstDay + 10) / 7).floor();

if (weekNumber < 1) {
// Die Woche gehört zum letzten Jahr.
return DateTime(year - 1, 12, 31).weekOfYear;
} else if (weekNumber > 52) {
// Prüfe, ob die Woche zum nächsten Jahr gehört.
int daysInYear = DateTime(year, 12, 31).difference(DateTime(year, 1, 1)).inDays + 1;
int threshold = (daysInYear - 31 + weekdayOfFirstDay - 10) ~/ 7;
if (weekNumber > threshold) return 1;
}
return weekNumber;
}
}