-
Notifications
You must be signed in to change notification settings - Fork 12
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
Calling getWeek method throws data type exception #5
Comments
Hi @ImAliSuleiman, even I am facing the same issue. Is there any workaround for this problem:
My Console:
|
I'm using a custom class that computes a week number since the start of a year from the date passed: class DateTimeHelper {
static int currentWeek() {
return weekOfYear(DateTime.now());
}
static int weekOfYear(DateTime date) {
DateTime monday = weekStart(date);
DateTime first = weekYearStartDate(monday.year);
int week = 1 + (monday.difference(first).inDays / 7).floor();
if (week == 53 && DateTime(monday.year, 12, 31).weekday < 4) week = 1;
return week;
}
static DateTime weekStart(DateTime date) {
// This is ugly, but to avoid problems with daylight saving
DateTime monday = DateTime.utc(date.year, date.month, date.day);
monday = monday.subtract(Duration(days: monday.weekday - 1));
return monday;
}
static DateTime weekEnd(DateTime date) {
// This is ugly, but to avoid problems with daylight saving
// Set the last microsecond to really be the end of the week
DateTime sunday =
DateTime.utc(date.year, date.month, date.day, 23, 59, 59, 999, 999999);
sunday = sunday.add(Duration(days: 7 - sunday.weekday));
return sunday;
}
static DateTime weekYearStartDate(int year) {
final firstDayOfYear = DateTime.utc(year, 1, 1);
final dayOfWeek = firstDayOfYear.weekday;
return firstDayOfYear.add(
Duration(days: (dayOfWeek <= DateTime.thursday ? 1 : 8) - dayOfWeek));
}
} You can download the class file below. Usage:
var todayWeek = DateTimeHelper.currentWeek();
print('todayWeek=' + todayWeek.toString());
var otherWeek = DateTimeHelper.(new DateTime(2000, 6, 5)); // for Jun 5, 2000
print('otherWeek =' + otherWeek.toString()); I hope that helps you @sandeepyohans. For anything - regarding code, of course - feel free to reach out. |
Thanks for your reply @ImAliSuleiman This helps. |
Calling
dateUtil.getWeek()
throws an error with a message "type 'double' is not a subtype of type 'int'
".A portion of code that raises the exception is as follows:
DateTime today = DateTime.now();
var todayWeek = dateUtil.getWeek(today.month, today.day, today.year);
print('todayWeek=' + todayWeek.toString());
The text was updated successfully, but these errors were encountered: