如何在 dart 中获得一周的开始或结束时间?一个例子是,如果三天前是星期一,今天是星期三,我如何使用 dart 找到一周的开始,即星期一
lrn*_*lrn 42
DartDateTime有一个weekday吸气剂,周一为 1,周日为 7。使用它,我会这样做:
DateTime mostRecentSunday(DateTime date) =>
DateTime(date.year, date.month, date.day - date.weekday % 7);
Run Code Online (Sandbox Code Playgroud)
获取最近的星期日(如果一周从星期日开始,则为本周的开始),以及
DateTime mostRecentMonday(DateTime date) =>
DateTime(date.year, date.month, date.day - (date.weekday - 1));
Run Code Online (Sandbox Code Playgroud)
最近的星期一(如果一周从星期一开始,则为本周的开始)。
你可以概括为
/// The [weekday] may be 0 for Sunday, 1 for Monday, etc. up to 7 for Sunday.
DateTime mostRecentWeekday(DateTime date, int weekday) =>
DateTime(date.year, date.month, date.day - (date.weekday - weekday) % 7);
Run Code Online (Sandbox Code Playgroud)
如果您打算将结果用作日历日期,我将使用DateTime.utc构造函数。(始终使用 UTC 表示日历日期,然后您可以安全地对它们进行基于天的算术)。
我什至会考虑DateTime.utc在任何情况下使用,因为它避免了从午夜开始的夏令时的任何潜在问题(很少见,但由于时区,即使不太可能的事情也往往在某个时刻发生在某个地方)。
jul*_*101 17
您可以使用https://api.dart.dev/stable/2.5.1/dart-core/DateTime/weekday.html从 DateTime 获取工作日,并从您的日期中添加/减去此数字:
void main() {
final date = DateTime.parse('2019-10-08 15:43:03.887');
print('Date: $date');
print('Start of week: ${getDate(date.subtract(Duration(days: date.weekday - 1)))}');
print('End of week: ${getDate(date.add(Duration(days: DateTime.daysPerWeek - date.weekday)))}');
}
DateTime getDate(DateTime d) => DateTime(d.year, d.month, d.day);
Run Code Online (Sandbox Code Playgroud)
更新
请阅读 lrn 的答案并投票。他比我更了解这些东西。:)
M E*_*B O 16
一周的第一天
DateTime findFirstDateOfTheWeek(DateTime dateTime) {
return dateTime.subtract(Duration(days: dateTime.weekday - 1));
}
Run Code Online (Sandbox Code Playgroud)
本周最后一天
DateTime findLastDateOfTheWeek(DateTime dateTime) {
return dateTime
.add(Duration(days: DateTime.daysPerWeek - dateTime.weekday));
}
Run Code Online (Sandbox Code Playgroud)
本月最后一天
DateTime findLastDateOfTheMonth(DateTime dateTime) {
return DateTime(dateTime.year, dateTime.month + 1, 0);
}
Run Code Online (Sandbox Code Playgroud)
每个月的第一天
DateTime findFirstDateOfTheMonth(DateTime dateTime) {
return DateTime(dateTime.year, dateTime.month, 1);
}
Run Code Online (Sandbox Code Playgroud)
一年的最后一天
DateTime findLastDateOfTheYear(DateTime dateTime) {
return DateTime(dateTime.year, 12, 31);
}
Run Code Online (Sandbox Code Playgroud)
新年第一天
DateTime findFirstDateOfTheYear(DateTime dateTime) {
return DateTime(dateTime.year, 1, 1); }
Run Code Online (Sandbox Code Playgroud)
https://api.dart.dev/stable/2.5.1/dart-core/DateTime-class.html
您执行此操作的方式可能取决于您的应用程序的本地化。如果您想将星期日视为一周的开始,您可以这样做:
DateTime now = DateTime.now();
int currentDay = now.weekday;
DateTime firstDayOfWeek = now.subtract(Duration(days: currentDay));
Run Code Online (Sandbox Code Playgroud)
如果您将星期一作为开始,请执行以下操作:
DateTime now = DateTime.now();
int currentDay = now.weekday;
DateTime firstDayOfWeek = now.subtract(Duration(days: currentDay - 1));
Run Code Online (Sandbox Code Playgroud)
如果您在 Flutter 中使用 Dart,则可以使用MaterialLocalizations类来获取一周第一天的索引(0 = 星期日,6 = 星期六)。这应该可以帮助您决定应该使用上面的哪种方法。
为firstDayOfWeekIndex属性给出的示例是一个很好的参考:
var localizations = MaterialLocalizations.of(context);
// The name of the first day of week for the current locale.
var firstDayOfWeek = localizations.narrowWeekdays[localizations.firstDayOfWeekIndex];
Run Code Online (Sandbox Code Playgroud)
MaterialLocalizations 类附带了大量用于格式化和显示日期时间的内置方法。
日期时间: 2021-01-26 11:21:05.429320
如果这些都不满足您的需求,您还可以使用DateFormat类来指定 DateTime 应如何显示。请务必注意,DateFormat 类对日期的索引不同,其中 1 = 星期一,7 = 星期日。
DateFormat.yMMMd().format(new DateTime.now()) // Jan 26, 2021
DateFormat(DateFormat.ABBR_MONTH_DAY).format(now) // Jan 26
DateFormat(DateFormat.WEEKDAY).format(now) // Tuesday
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5149 次 |
| 最近记录: |