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

Calling getWeek method throws data type exception #5

Open
MzeeAlii opened this issue Oct 11, 2019 · 3 comments
Open

Calling getWeek method throws data type exception #5

MzeeAlii opened this issue Oct 11, 2019 · 3 comments

Comments

@MzeeAlii
Copy link

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());

@sandeepyohans
Copy link

Hi @ImAliSuleiman, even I am facing the same issue. Is there any workaround for this problem:
My code:

  DateTime now = new DateTime.now();
  DateUtil util = new DateUtil();
  var week =  util.getWeek(now.month, now.day, now.year) ;
  print(week);

My Console:

Unhandled exception:
type 'double' is not a subtype of type 'int'
#0      DateUtil.getWeek (package:date_util/src/dateUtil_base.dart:181:9)

@MzeeAlii
Copy link
Author

MzeeAlii commented Mar 9, 2020

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.
date_time_helper.dart.zip

Usage:

  • To get a week number of the current year, just call currentWeek function and it does magic for you.
var todayWeek = DateTimeHelper.currentWeek();
print('todayWeek=' + todayWeek.toString());
  • To get week number of any date (other than the current date), call weekOfYear function while passing date to get its week number.
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.

@sandeepyohans
Copy link

Thanks for your reply @ImAliSuleiman This helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants