获取当前月份的星期数

时间:2020-01-21 11:04:51

标签: flutter dart

如何在Dart中获取数字中的当前星期几?我需要创建一种带有周视图的日历,其中显示“ 2. Week of January”之类的内容。

1 个答案:

答案 0 :(得分:1)

您可以使用DateTime()。now()来获取系统的当前时间和日期或今天的日期。这是下面的代码片段:

// Current date and time of system
String date = DateTime.now().toString();

// This will generate the time and date for first day of month
String firstDay = date.substring(0, 7) + '01' + date.substring(10);

// week day for the first day of the month
int weekDay = DateTime.parse(firstDay).weekday;

// If your calender starts from sunday
if (weekDay == 7) {
      // Current week
      int currentWeek = (DateTime.now().day / 7).ceil();
 }else{
      // Current week
      int currentWeek = ( (DateTime.now().day + weekDay) / 7).ceil();
 }

或者,如果要查找日历月份用户界面的完整实现,则click here