jiffy icon indicating copy to clipboard operation
jiffy copied to clipboard

Week days and week count

Open talamaska opened this issue 6 years ago • 13 comments

I'm not sure why is this implemented that way, for me this is incorrect, everybody knows a week starts from Monday, but instead your week day is 0-6 Sunday to Saturday? Next big thing with the same logic, one record is on Saturday, next one is on Sunday and your logic makes them in different weeks. This is wrong. Maybe consider having a setting to set which is the first day of the week so that the calculations are correct. This is a show stopper for me. I'm trying to group records by week to show a bar chart, but I'm getting wrong results.

talamaska avatar Apr 05 '20 10:04 talamaska

@talamaska This is quite standard when working with dates, in Javascript it is exactly behaving the same ways. Weeks start on Sunday and end on Saturday; They also start with Sunday = 0 and Saturday = 6; Same goes for Month, they start at 0 and end at 11.

schankam avatar Apr 09 '20 17:04 schankam

@schankam Then why in the original DateTime in Dart Monday is 1 and Sunday is 7. Why not make that consistent with the Dart API instead of implementing something from Javascript which is different than in Dart, this is confusing to say at least. https://api.dart.dev/stable/2.7.2/dart-core/DateTime/weekday.html Same goes for Month https://api.dart.dev/stable/2.7.2/dart-core/DateTime/month.html 1 - 12 this is a Dart library not Javascript

talamaska avatar Apr 09 '20 21:04 talamaska

Is there some way, to change week starting day?

andzejsw avatar Apr 28 '20 10:04 andzejsw

@talamaska I agree with you and at first I was basing my comment on what I saw on the documentation, but what you're saying indeed makes more sens.

schankam avatar Apr 28 '20 12:04 schankam

@talamaska @schankam I understand it was my mistake on my side. That the numbering of the weekdays is not well implemented based on Dart. That is the numbering should be from 1 -7 and not 0 - 6

But on the question on which day to start, it was based on locale and I chose the most used that is Sunday to Saturday. And this lead to other locales not getting the correct start day. It was later updated, a few months ago, where the start date would change based on the locale you chose e.g

await Jiffy.locale("fr"); // the start date would be Sunday await Jiffy.locale("ar"); // the start date would be Saturday

But all in all, we should implement as @talamaska suggested a setting for this, on which you can choose which start day you want

jama5262 avatar Apr 29 '20 08:04 jama5262

ok, that information was missing from the docs. I didn't know that it depends on the locale. But still, it's kind of strange when you make some comparisons, in one locale to show that they are on the same week, and on the other to show that they are not. Thanks for explaining that behavior.

talamaska avatar Apr 29 '20 14:04 talamaska

Does anyone know if this is implemented yet? Jiffy().week is giving me the wrong week, I would like to use Monday-Sunday.

Adrian4777 avatar Sep 06 '20 22:09 Adrian4777

Does anyone know if this is implemented yet? Jiffy().week is giving me the wrong week, I would like to use Monday-Sunday.

Me too! Please implement!

akeblom avatar Nov 04 '20 14:11 akeblom

@akeblom, try this:

/// Calculates week number from a date as per https://en.wikipedia.org/wiki/ISO_week_date#Calculation
int weekNumber(DateTime date) {
  int dayOfYear = int.parse(DateFormat("D").format(date));
  int week = ((dayOfYear - date.weekday + 10) / 7).floor();
  if (week == 0) {
    return 53;
  } else {
    return week;
  }
}

Adrian4777 avatar Nov 04 '20 14:11 Adrian4777

@akeblom, try this:

/// Calculates week number from a date as per https://en.wikipedia.org/wiki/ISO_week_date#Calculation
int weekNumber(DateTime date) {
  int dayOfYear = int.parse(DateFormat("D").format(date));
  int week = ((dayOfYear - date.weekday + 10) / 7).floor();
  if (week == 0) {
    return 53;
  } else {
    return week;
  }
}

Thanks!

akeblom avatar Nov 05 '20 13:11 akeblom

@schankam @talamaska : The start of the week is locale dependent. I.e. in the US the week starts on a Sunday, but in Sweden the week starts on a Monday. Since I live in Sweden and have 'sv' as locale, I would like to see the Jiffy(date).startOf(Units.WEEK).dateTime give me the date of the Monday for that week. Now I get Sunday...

SuperKrallan avatar May 02 '21 06:05 SuperKrallan

...and I also found and had to implement Adrian4777s solution above for the week number. And in this context I also found the following code snippet that I liked and implemented:

/// Calculates number of weeks for a given year as per https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year int numOfWeeks(int year) { DateTime dec28 = DateTime(year, 12, 28); int dayOfDec28 = int.parse(DateFormat("D").format(dec28)); return ((dayOfDec28 - dec28.weekday + 10) / 7).floor(); }

SuperKrallan avatar May 02 '21 06:05 SuperKrallan

It would be very helpful to set the locale and start of the week to the specific instance. We would use this for calculating dates based on work/school weeks where we want the weekend to include Saturday/Sunday. This would override the locale for that usage. The global setting should remain the default for the locale.

danielmahon avatar Sep 24 '22 04:09 danielmahon