我正試圖按照照片中所示的日期進行標注。第一個星期天過后,標注的數字達到了20s。第2個星期天過后,標注的數字達到了30多歲。后面的數字代表一周中的哪一天。例如,25代表這個月的第二個星期五。
關于如何創建這個邏輯,有什么建議嗎?
uj5u.com熱心網友回復:
您可以使用DateTime類來實作。我已經添加了一個示例實作,你可以進一步優化:
// Function that maps day with corresponding value。
Map<int, int> generateMap(DateTime firstDateOfMonth) {
final map = <int, int>{};
final numOfDays = getNumberOfDaysInMonth(firstDateOfMonth)。
int weekDay = firstDateOfMonth.weekday。
int sunday = (7 - weekDay) 1;
int week = 1;
for (int i = 1; i <= numOfDays; i ) {
if (sunday < i) {
weekvv = 1;
sunday = 7;
weekDay = 1;
}
map[i] = (week * 10) weekDay;
weekDay ;
}
return map。
}
//回傳一個月內的天數。
int getNumberOfDaysInMonth(DateTime date) {
int numOfDays = 0;
switch (date.month) {
case DateTime.January:
case DateTime.march:
case DateTime.May:
case DateTime.july。
case DateTime.august:
case DateTime.October:
case DateTime.december:
numOfDays = 31;
break;
case DateTime.april:
case DateTime.june。
case DateTime.september:
case DateTime.november:
numOfDays = 30;
break;
case DateTime.february:
isLeapYear(date)? 29 : 28;
break;
}
return numOfDays;
}
//檢查該年是否為閏年。
bool isLeapYear(DateTime date) => date.year % 4 == 0;
你可以通過每月的第一天來生成該月的映射:
你可以通過每月的第一天來生成該月的映射。
final now = DateTime.now()。
final firstDayOfMonth = DateTime(now.year, now.month);
print(generateMap(firstDayOfMonth))。
對于當前月份(2021年9月),它列印出以下輸出結果:
{
1: 13, 2: 14, 3: 15, 4: 16, 5: 17,
6: 21, 7: 22, 8: 23, 9: 24, 10: 25,
11: 26, 12: 27, 13: 31, 14: 32, 15: 33,
16: 34, 17: 35, 18: 36, 19: 37, 20: 41,
21: 42, 22: 43, 23: 44, 24: 45, 25: 46,
26: 47, 27: 51, 28: 52, 29: 53, 30: 54
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/332974.html
標籤:

