如何在Dart中获取月份中的当前星期?我需要使用周 View 创建某种日历,其中应显示“2. Week of January”之类的内容。
最佳答案
您可以使用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
关于flutter - 获取当前月份的星期数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59839755/