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

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

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 select a week(custom) in the Flutter Date Range Picker (SfDateRangePicker)

I try to implement a weekly selector by following this example - https://www.syncfusion.com/kb/11412/how-to-select-a-week-in-the-flutter-date-range-picker-sfdaterangepicker
The problem I encounter is that the "args.value" from DateRangePickerSelectionChangedArgs returns a date range from Sunday to Saturday. What I want is for the DateRangePickerSelectionChangedArgs to return a date range from Monday to Sunday. I want the weekly selector to select the whole week from Monday to Sunday not from Sunday to Saturday as shown in this screenshot.
enter image description here
I try the codes below. I tried adding one to the start date and to the end date so that Sunday becomes Monday and Saturday becomes Sunday, but the code didn't work when I did that.
void selectionChanged(DateRangePickerSelectionChangedArgs args) {
isSameDate(date1, date2) {
if (date2 == date1) {
return true;
}
if (date1 == null || date2 == null) {
return false;
}
return date1.month == date2.month && date1.year == date2.year && date1.day == date2.day;
}
int firstDayOfWeek = DateTime.sunday % 7;
int endDayOfWeek = (firstDayOfWeek - 1) % 7;
endDayOfWeek = endDayOfWeek < 0 ? 7 + endDayOfWeek : endDayOfWeek;
PickerDateRange ranges = args.value;
DateTime date1 = ranges.startDate!;
DateTime date2 = (ranges.endDate ?? ranges.startDate)!;
if (date1.isAfter(date2)) {
var date = date1;
date1 = date2;
date2 = date;
}
int day1 = date1.weekday % 7;
int day2 = date2.weekday % 7;
DateTime dat1 = date1.add(Duration(days: (firstDayOfWeek - day1) + 1));
DateTime dat2 = date2.add(Duration(days: (endDayOfWeek - day2) + 1));
if (!isSameDate(dat1, date1) || !isSameDate(dat2, date2)) {
datePickerController.selectedRange = PickerDateRange(dat1, dat2);
}
}
You can customize the first day of the week in the date range picker by using the FirstDayofWeek property from the MonthViewSettings. Refer to the UG documentation to know more about FirstDayofWeek,
https://help.syncfusion.com/flutter/daterangepicker/getting-started#change-first-day-of-week
Refer to the code snippets to achieve your requirement,

how to solve using dart .Given a year, return the century it is in

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.
Example
For year = 1905, the output should be
solution(year) = 20;
For year = 1700, the output should be
solution(year) = 17.
Input/Output
[execution time limit] 4 seconds (dart)
[input] integer year
A positive integer, designating the year.
Guaranteed constraints:
1 ≤ year ≤ 2005.
[output] integer
The number of the century the year is in.
int getCentury(int year) => (year - 1) ~/ 100 + 1;
is this what you want?
void main() {
int year1 = 1905;
int year2 = 1700;
print('year1 : ${getCentury(year1)}'); //20
print('year2 : ${getCentury(year2)}'); //17
}
int getCentury(int year){
if(1 > year || 2005 < year){
return 0;
}
int century = year ~/ 100;
int temp = year % 100;
if(temp != 0){
century += 1;
}
return century;
}

Flutter: Find the difference between two dates in Years, Months including leap years

I am working on an application in flutter where I have to get the difference between two dates in Years, Months and Dates
like below Ex.
final _date1 = DateTime(2019, 10, 15);
final _date2 = DateTime(2020, 12, 20);
Answer Difference = 1 Year, 2 Month, 5 Days
I searched a lot but unfortunately din't get a proper solution to calculate the difference between two dates in Years, Months including leap years.
Note: I know how to get the difference between two dates in Days i.e
final _bd = DateTime(2020, 10, 12);
final _date = DateTime.now();
final _difference = _date.difference(_bd).inDays;
You may customize the following code as per your requirement to return a string or object or make it an extension:
void getDiffYMD(DateTime then, DateTime now) {
int years = now.year - then.year;
int months = now.month - then.month;
int days = now.day - then.day;
if (months < 0 || (months == 0 && days < 0)) {
years--;
months += (days < 0 ? 11 : 12);
}
if (days < 0) {
final monthAgo = DateTime(now.year, now.month - 1, then.day);
days = now.difference(monthAgo).inDays + 1;
}
print('$years years $months months $days days');
}

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