Get DateTime by loop with 2 months gap - flutter

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);

Related

Flutter how to get dates from week number

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);
}

how to create a 30 minutes timeslot.. Im getting the start date and end date from backend

List<WorkingHourModel> workingHours = response['hours'];
int startTime = int.parse(workingHours[0].startTime.split(':')[0]);
int endTime = int.parse(workingHours[0].endTime.split(':')[0]);
int hoursLeft = endTime - startTime + 1;
List<String> hours =
List.generate(hoursLeft, (i) => '${(endTime - i)}:00').reversed
.toList();
print('Hours-------- $hours');
return {
'success': true,
'hours': hours,
};
}
//this will give the time slots as '1:00', '2:00', '3:00', '4:00'
//Start time will be 1:00 and end time will be 4:00.
Heading
But i need 30 mins breakage, like,
'1:00', '1:30', '2:00', '2:30', '3:00', '3:30', '4:00'
Kindly help..
This should generate the list as you want it:
void main(List<String> args) {
// lets assume 9 to 5
int startTime = 9;
int endTime = 17;
int hoursLeft = endTime - startTime + 1;
final hours = List.generate(hoursLeft * 2, (i) => '${endTime - (i/2).truncate()}:${i%2 == 1 ? '00' : '30'}').reversed.toList();
print('Hours-------- $hours');
}
Hours-------- [9:00, 9:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30]
Generally speaking, if you have more complicated calculations (what happens if someone works 18:00 to 05:00 in the nightshift?) you may want to use the proper datatypes instead of integers and string concatenation.

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;
}

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);