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

New methods on DateTime (lastDayOfMonth, lastDayOfWeek, firstDayOfWeek) #48943

Closed
FMorschel opened this issue May 2, 2022 · 4 comments
Closed
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug

Comments

@FMorschel
Copy link
Contributor

Looking for lastDayOfMonth like feature, found this StackOverflow question and I really liked the way Yogesh Parwani mixed Chris Buckett and Kai Sellgren awnsers. As pointed by Juniper Belmont on Buckett's answer, I do think this could be a new feature or at least pointed on the official documentation that these values could be obtained like this.

Code bellow:

extension DateTimeExtension on DateTime {

  DateTime get firstDayOfWeek => subtract(Duration(days: weekday - 1));

  DateTime get lastDayOfWeek => add(Duration(days: DateTime.daysPerWeek - weekday));

  DateTime get lastDayOfMonth => month < 12 ? DateTime(year, month + 1, 0) : DateTime(year + 1, 1, 0);
}
@natebosch natebosch transferred this issue from dart-lang/language May 2, 2022
@natebosch natebosch added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug and removed feature labels May 2, 2022
@natebosch
Copy link
Member

cc @lrhn for thoughts

My initial reaction is we probably wouldn't put this in the SDK. I could imagine putting this into a utility package, but it's not a good fit for any of our existing core packages.

@lrhn
Copy link
Member

lrhn commented May 3, 2022

We don't have a package:core, which is where it would fit.

I'm not completely opposed to putting DateTime extensions in dart:core, but I don't see this as features that will be widely used, which is one of the criteria for going into a dart: library (widely used by many users, or heavily used by some users, and something you'll only ever need one canonical version of, so we might as well provide it - or something which requires native interop with the runtime system).

The "firstDayOfWeek" is contentious, since some people (incorrectly 😛) consider Sunday the first day of the week, even though it is obviously Monday. That suggests having separate packages that you can choose between.

Maybe we should have a package:calendar (name currently taken by a 4-year-stale package.)

(Also, don't use Duration when calculating dates. Better implementation:

extension DateTimeFirstLast on DateTime {
  DateTime get firstDayOfWeek => DateTime.utc(year, month, day + 1 - weekday);
  DateTime get lastDayOfWeek => DateTime.utc(year, month, day + 7 - weekday);  
  DateTime get firstDayOfMonth => DateTime.utc(year, month, 1);
  DateTime get lastDayOfMonth => DateTime.utc(year, month + 1, 0);
  DateTime get firstDayOfYear => DateTime.utc(year, 1, 1);
  DateTime get lastDayOfYear => DateTime.utc(year, 12, 31);
}

which isn't affected by the time of the DateTime object it's called on, and uses UTC to represent time-less dates as recommended.)

@mnordine
Copy link
Contributor

mnordine commented May 3, 2022

@FMorschel you should submit a PR to the time package: https://github.com/jogboms/time.dart

@FMorschel
Copy link
Contributor Author

As this request is now on https://github.com/jogboms/time.dart as suggested, I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants