Flutter how to get dates from week number - flutter

I am trying to get dates from week number in flutter. But I dont know how to do it. I got the week number and the days of the week. Now I want to get the dates from the days in this week.
For example:
week 2 Monday date,
week 2 Tuesday date,
week 2 Friday date
I have started with this code but I need to solve it in another way.
int weeknumber = 2;
//1=monday, 2=tuesday, 5=friday
List<int> weekdays = [1,2,5];
int year = 2023;
int totaldays = weeknumber * 7;
final extraDuration = Duration(days: totaldays);
final startDate = DateTime(year);
final dates = startDate.add(extraDuration);
print(dates);
Here is the output: 2023-01-15 00:00:00.000
But I want: 2023-01-09, 2023-01-10, 2023-01-13
What can I do to get these dates?

If you want this output
Monday:2023-01-09 00:00:00.000
Tuesday:2023-01-10 00:00:00.000
Friday:2023-01-13 00:00:00.000
you can work on this code
import 'package:intl/intl.dart';
void main() {
int weeknumber = 2;
//1=monday, 2=tuesday, 5=friday
List<String> weekdays = ['Monday','Tuesday','Friday'];
int year = 2023;
int firstDayOfWeek = (weeknumber-1) * 7;
final extraDuration = Duration(days: firstDayOfWeek);
final startDate = DateTime(year);
final dates = startDate.add(extraDuration);
for (var i = 0; i < 7; i++) {
var newDate = dates.add(Duration(days: i));
if(DateFormat('EEEE').format(newDate) == 'Monday')print('Monday:'+newDate.toString());
else if(DateFormat('EEEE').format(newDate) == 'Tuesday')print('Tuesday:'+newDate.toString());
else if(DateFormat('EEEE').format(newDate) == 'Friday')print('Friday:'+newDate.toString());
}
print(dates);
}

Related

Get DateTime by loop with 2 months gap

Hi guys i would like some help how can i achieve to get the date with a gap of 2 months or more so far i did on the code below
final dateNow = DateTime.now();
DateTime biMonth(DateTime? timeDate, int? add){
DateTime result = DateTime(dateNow.year,dateNow.month+ add! , dateNow.day);
return result;
}
/// The Function
getDatesGaps(){
DateTime timeNow = DateTime.now();
for(int p = 1 ; p < 3 ; p++){
timeNow = biMonth(timeNow,p);
log(timeNow.toString());
}
}
the result i get
2022-06-19 15:55:00.000
2022-07-19 15:55:00.000
result i want is like this
2022-06-19 15:55:00.000
2022-08-19 15:55:00.000
have a gap with 2 months.
You could just do
timeNow = biMonth(timeNow,p * 2);

display a periodic date in 1 month dart language

How do I display a periodic date in 1 month, for example, I want a date that appears every 2 days, for example starting from Month 04-2020
2020-04-01
2020-04-03
2020-04-06
Try this code below:
var date = DateTime.fromMicrosecondsSinceEpoch(200000); // initial date
for (int i = 1; i<=5; i++) {
var dateCopy = date;
dateCopy = dateCopy.add(Duration(days: i * 2));
print(dateCopy);
}

How many weeks in a month with X weekday in them?

I'm looking for a way to get the number of weeks with X weekday in them a month has. For example:- How many weeks are there in march with Wednesday in them? Answer - 5.
I would be happy with a general answer but it would be great to know if there's an efficient way to do that using dart. Thank you.
Try this...
int weeksCountOfWeekday(int weekdayToCnt, int month, int year) {
assert(weekdayToCnt >= 1 && weekdayToCnt <= 7);
final DateTime startDate = DateTime(year, month);
final DateTime endDate =
DateTime(year, month + 1).subtract(const Duration(days: 1));
int res = 1;
int dayOfWeekday;
if (weekdayToCnt < startDate.weekday) {
dayOfWeekday = startDate.day + 7 - (startDate.weekday - weekdayToCnt);
} else {
dayOfWeekday = 1 + weekdayToCnt - startDate.weekday;
}
while (dayOfWeekday + 7 <= endDate.day) {
res++;
dayOfWeekday += 7;
}
return res;
}

Flutter: Get Wrong days of the week

from Monday - Thursday, I get the right days, but from Friday, I get the wrong days. Why?
Code example:
Text(DateFormat('EEEE').format(DateTime(DateTime.friday))),
And i get Saturday. Is that a bug?
It's not a bug, DateTime() default constructor takes year as the first argument:
DateTime(int year,
[int month = 1,
int day = 1,
int hour = 0,
int minute = 0,
int second = 0,
int millisecond = 0,
int microsecond = 0])
: this._internal(year, month, day, hour, minute, second, millisecond,
microsecond, false);
So this code:
DateTime date = DateTime(DateTime.friday);
Is essentially constructing a DateTime of the year 5, because DateTime.friday is nothing more than a const int that equals to 5:
static const int monday = 1;
static const int tuesday = 2;
static const int wednesday = 3;
static const int thursday = 4;
static const int friday = 5;
static const int saturday = 6;
static const int sunday = 7;
static const int daysPerWeek = 7;
Formatting with DateFormat returns Saturday which happens to be the first day of the year:
import 'dart:core';
import 'package:intl/intl.dart';
main() {
DateTime date = DateTime(DateTime.friday); // creates a DateTime of the year 5
print(date.year); // year: 5
print(date.month); // month: Jan (default = 1)
print(date.weekday); // day: Saturday (first day of the year)
print("${date.year}-${date.month}-${date.day}"); // 5-1-1
}
DateTime should be used to define a specific Date and Time, for example, Friday 11th Dec 2020, it can't be used to define any Friday.

Can this be done using LINQ/Lambda, C#3.0

Objective: Generate dates based on Week Numbers
Input: StartDate, WeekNumber
Output: List of dates from the Week number specified till the StartDate
i.e. If startdate is 23rd April, 2010 and the week number is 1, then the program should return the dates from 16th April, 2010 till the startddate.
The function
public List<DateTime> GetDates(DateTime startDate,int weeks)
{
List<DateTime> dt = new List<DateTime>();
int days = weeks * 7;
DateTime endDate = startDate.AddDays(-days);
TimeSpan ts = startDate.Subtract(endDate);
for (int i = 0; i <= ts.Days; i++)
{
DateTime dt1 = endDate.AddDays(i);
dt.Add(dt1);
}
return dt;
}
I am calling this function as
DateTime StartDate = DateTime.ParseExact("20100423", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
List<DateTime> dtList = GetDates(StartDate, 1);
The program is working fine.
Question is using C# 3.0 feature like Linq, Lambda etc. can I rewrite the program.
Why? Because I am learning linq and lambda and want to implement the same. But as of now the knowledge is not sufficient to do the same by myself.
Thanks.
Something like this:
public IEnumerable<DateTime> GetDates2(DateTime startDate, int weeks)
{
var days = weeks * 7;
return Enumerable.Range(-days, days + 1).Select(i => startDate.AddDays(i));
}
The Enumerable.Range method will return a sequence of integers within a specified range, in your example from -7 to 0.
After that I simply use each integer to substract that number of days from your initial startDate, building an IEnumerable<DateTime>.
You can try something like
int weeks = 1;
var days = from d in Enumerable.Range(-weeks * 7, weeks * 7 + 1)
select StartDate.AddDays(d);