DateUtil (Flutter,Dart)

Dhanraj Verma
Jun 25, 2021
Photo by Nathan Dumlao on Unsplash

I was stuck in a problem when I was write code for one of my project. The Problem was, for a given date you have to extract whole week in which the given date falls. Whole week means a list of length 7 of type DateTime. I came with the solution below.

List<DateTime> weekOfDate(DateTime date){
print(date.weekday);
var week = <DateTime>[];
var suffixDays=7-date.weekday;
var prefixDays=7-(suffixDays+1);
for(int i=prefixDays;i>=1;i--){
week.add(DateUtils.addDaysToDate(date, -i));
}
week.add(date);
for(int i=1;i<=suffixDays;i++){
week.add(DateUtils.addDaysToDate(date, i));
}
return week;
}

--

--

Dhanraj Verma
0 Followers

I am working as a mobile application developer using flutter framework. I have done Master of computer application.