Group dates by week during multiple years - c#

I have a list of DateTime values and I want to split the whole list into sublists for each week.
The dates might span across multiple years(the user selects the start and end date), so a solution that splits them up per calendar week number would end up in conflicts.
Is there a way to traverse the list and then store each week's DateTime values in a new 2d list?
The values available are from Monday to Friday and first and last week might have fewer values.
The only relevant question I found is How to group dates by weeks but it is not suitable for my case.

You can use this method to get the week-number of a given DateTime. Then you can use Enumerable.GroupBy with an anonymous type containing the year and the weeknum:
var yearWeekGroups = allDates.GroupBy(d => new { d.Year, WeekNum = GetIso8601WeekOfYear(d) });
If you want a List<List<DateTime>> where each sub-list contains the dates of a week:
List<List<DateTime>> allWeeks = yearWeekGroups.Select(g => g.ToList()).ToList();
If your country doesn't use ISO 8601 you can use Calendar.GetWeekOfYear:
var cc = CultureInfo.CurrentCulture;
var yearWeekGroups = allDates.GroupBy(d => new
{
d.Year,
WeekNum = currentCulture.Calendar.GetWeekOfYear(d, cc.DateTimeFormat.CalendarWeekRule, cc.DateTimeFormat.FirstDayOfWeek)
});

Related

Check specific days exist in a set range of given weeks and add to list

What would be the best choice in getting and returning a list of specific dates between a 3 week range?
My intent is to create delivery dates based on a delivery centre's given days they are available
public List<DayOfWeek> DeliveryDays { get; set; }
DeliveryDays contains set values from 0-6 (0 being Sunday, 1 Monday, etc.)
I want to get those values, pass them through 3 weeks worth of following dates, and return those delivery days in a list (so only those centre's can order on select days).
Here's what I have so far:
public List<DateTime> CalculateAvailableDeliveryDates()
{
//Loop through 3 weeks of days
DateTime today = DateTime.Today; //Specify today's date
DateTime totalDateCount = today.AddDays(1); //Plus one day on each time counted
var dates = Enumerable.Range(0, 21).Select(days => totalDateCount.AddDays(days)).ToList(); //Count from 0 to 21 (3 weeks worth of days). On each count, run totalDateCount
//if exists in deliveryDayList
//add to dates via dates.Add func
if (DeliveryDays.Contains(DayOfWeek.Monday))
{
//action
} //and so on for each day
//return specific dates from date range
return dates;
}
currently I get a readout of 21 days. The if statement does nothing and is only serving as an example of my logic.
Would the best method be: rather than getting a list first, to do a check and nest if/case statements based on the DeliveryDates per centre and then return them into a list?
Thanks in advance.
Given a list of DayOfWeek, you can select all dates in the next 21 days that match one of those days of the week using System.Linq. The Enumerable.Range selects a range of numbers, Select will then select a bunch of DateTime objects representing Today plus some number of days, and Where is used to filter the results, comparing the DayOfWeek for each date to see if it exists in DeliveryDays:
List<DayOfWeek> DeliveryDays = new List<DayOfWeek>();
public List<DateTime> GetAvailableDeliveryDates()
{
// 1. Get a range of numbers representing the days to add
// to today, which will make up our range of dates
// 2. Select a date using Today.AddDays for each number
// 3. Filter on only days which are contained in DeliveryDays
return Enumerable.Range(0, 21) // Define the range
.Select(i => DateTime.Today.AddDays(i)) // Select the range
.Where(date => DeliveryDays.Contains(date.DayOfWeek)) // Filter the range
.ToList();
}

Order date list for comparison

I previously asked this question here: List of Dates ordered in a certain way
I thought the proposed solution was fine until the year ticked over on my date list and encountered an issue.
My date list (in this string based format - this is how the data comes to me from the source API)
201711
201712
201801
201811
201812
201901
I want to present my data in a bar chart to show 3 months worth of year on year comparison in month order. This would mean I order the list as so
201711
201811
201712
201812
201801
201901
So I can then see year-on-year bars for November, December and Jan in that order.
I've tried the solution at the bottom of the question but it places the order like so (which is not what I want):
201801
201901
201711
201811
201712
201812
For clarity, next month it will need to move forward to be this date list:
the first month I want will always be 2 months before the current one
201712
201812
201801
201901
201802
201902
var rowsInOrder = dateList.OrderBy(n => DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Month).ThenBy(n=> DateTime.ParseExact(n, "yyyyMM", CultureInfo.CurrentCulture).Year);
You can use this Lookup approach that first determines the month-groups:
var monthLookup = dateList
.Select(s => new{String = s, Date = DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture)})
.OrderBy(x => x.Date) // not necessary in your sample data but i assume it's desired
.ToLookup(x=> x.Date.Month);
var rowsInOrder = monthLookup.SelectMany(x => x).Select(x => x.String);
I could manage to achieve your goal using GroupBy to group months,
var rowsInOrder = new List<string>();
foreach (var grouping in dates.GroupBy(s =>
DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month))
{
rowsInOrder.AddRange(grouping.OrderBy(s => s));
};
You can also order months with same logic:
var rowsInOrder = new List<string>();
foreach (var grouping in dates
.OrderBy(s => DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month).GroupBy(s =>
DateTime.ParseExact(s, "yyyyMM", CultureInfo.CurrentCulture).Month))
{
rowsInOrder.AddRange(grouping.OrderBy(s => s));
}
It seems to me that this is sufficient:
var rowsInOrder = dates.OrderBy(x => x).GroupBy(x => x.Substring(4)).SelectMany(x => x);
There's simply no need to muck around with parsing dates. It's a simple string sort and group this way.
So you are stuck with a sequence of objects, where every object has a Date property of type string in the format yyyyMM. and you want to extract some data from it.
Your date format is language independent. It doesn't matter whether your computer is a British one, or a Chinese one. The Date property will always be in format yyyyMM.
This makes it fairly easy to convert it into a DateTime format, which makes it easy to access the Year and Month.
const string dateTimeFormat = "yyyyMM";
CultureInfo provider = CultureInfo.InvariantCulture;
var dateList = ... // your original list of items with the string Date property
var itemsWithYearMonth = dateList.Select(item => new
{
DateTime = DateTime.ParseExact(item, dateTimeFormat, provider)
... // select other items you need for your bar chart
});
Now, given a StartYear/Month, a NrOfYears and a NrOfMonths you want to group dateTimeItems into groups of same month.
For example, starting at 2018-11, I want groups with four months, for three consecutive years (yeah, yeah, I know in your original request it was only 3 months, 2 years, but why limit yourself to this, let's make your code re-usable):
group 1: 2018-11, 2019-11, 2020-11, 2021-11
group 2: 2018-12, 2019-12, 2020-12, 2021-12
group 3: 2019-01, 2020-01, 2021-01, 2022-01
Bonus-points: we'll pass the year boundary!
So input:
var itemsWithYearMonth = ... // see above
int startYear = ...
int startMonth = ...
int nrOfMonths = ...
int nrOfYears = ...
We will make groups of items with same months. We don't want all Months of the year, we only want some months. If we want the 3 months starting at month 11, we need to keep the groups with Months 11, 12, 1.
var desiredMonths = Enumerable.Range(startMonth, nrOfMonths) // example: 11, 12, 13
.Select(monthNr => 1 + ((monthNr-1) % 12)); // 11, 12, 1
From your input, we don't want all months, we only want the year/month larger than the starting month.
DateTime startMonth = new DateTime(startYear, startMonth, 1);
The easiest way is to keep only the input source of items with date equal or larger than startMonth and take only the first NumberOfYears items of each group. This way you get the correct number of items if you pass the year boundary like I did in my example.
var result = itemsWithYearMonth
// keep only the items newer than startMonth
.Where(item => item.DateTime >= startMonth)
// group by same month:
.GroupBy(item => item.DateTime.Month,
(month, itemsWithThisMonth) => new
{
Month = month, // in my example: 11, 12, 1, ...
// in every group: take the first nrOfYears items:
Items = itemsWithThisMonth
// order by ascending year
.OrderBy(itemWithThisMonth => itemWithThisMonth.Year)
// no need to order by Month, all Months are equal in this group
.Take(nrOfYears)
.ToList(),
})
// keep only the desired months:
.Select(group => desiredMonth.Contains(group.Month));
So now you have groups:
group of month 11, with data of years 2018, 2019, 2020, 2021
group of month 12, with data of years 2018, 2019, 2020, 2021
group of month 01, with data of years 2019, 2020, 2021, 2022

How to get the number of dates of a specific Day Of the week in a List<DateTime>?

Good day, I have a list, with some dates, I need to get The number of days of a day of the week, for example, get in a int variable the number of dates that are on Sunday, another int for Monday...
I've tried using the following code:
List<int> SundayDates = DateList.FindAll(x => x.DayOfWeek = DayOfWeek.Sunday)
but it says that it's read only, (And DateList, is the list where I storage my dates and where I want to get it).
You need to use ==, that is for equality checking. A single = is an assignment
To count the number of occurrences use Count.
int numberOfSundays = DateList.Count(x => x.DayOfWeek == DayOfWeek.Sunday);
Here is a count for each day of the week. The grouped join ensures that even if a day is not present in the list (example no occurrences of Monday) that it will still occur in the resulting list with a count of 0.
dotnetfiddle
var DateList = new List<DateTime>(); // your populated list of dates
var allDaysOfWeek = Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>();
var totalDayCounts = allDaysOfWeek.GroupJoin(DateList, dayOfWeek => dayOfWeek, date => date.DayOfWeek, (dayOfWeek, times) => new
{
DayOfTheWeek = dayOfWeek,
DayOfWeekCount = times.Count()
});
do not forget to add using System.Linq; at the top of your code file

Sorting a List of Month Names Starting with Current Month [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linq orderby, start with specific number, then return to lowest
I need to create a ComboBox that lists the months of the year, but need it to start with the
current month, and then the rest in month order, e.g.:
October
November
December
January
February
March
etc.....
The data source is a list of months in a database, which are numbered according to month number (i.e. January = 1, etc.) and then manipulated to give a datetime
How can I sort this list in C# so that I get the order I want?
TIA.
string[] months = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
var ordered = months.Skip(DateTime.Today.Month - 1)
.Concat(months.Take(DateTime.Today.Month - 1))
.Where(s=>!String.IsNullOrEmpty(s))
.ToList();
Use DateTimeFormatInfo.GetMonthName methoed
List<string> list = new List<string>();
DateTimeFormatInfo dtFI = new DateTimeFormatInfo();
DateTime currentDate = DateTime.Now;
DateTime nextyearDate = currentDate.AddYears(1).AddDays(-1);
while (currentDate < nextyearDate)
{
list.Add(dtFI.GetMonthName(currentDate.Month));
currentDate = currentDate.AddMonths(1);
}
This will create a new list of months, starting from current month.
Another take on this with LINQ:
// month name source, use what you prefer
var monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
var sorted = Enumerable.Range(1, 12).Zip(monthNames, Tuple.Create)
.OrderBy(t => (t.Item1 - DateTime.Today.Month + 12) % 12)
.Select(t => t.Item2)
.ToArray();
You can get the lists of months in C# with List<DateTime> monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames.ToList();. It get a List<DateTime> with 13 elements (a null month name at the end, but you can always remove the last element monthNames.RemoveAt(monthNames.Count - 1);
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.monthnames.aspx
To reorder this list you can get the current month number index using DateTime.Now.Month.ToString("00");, and restructure the list newMonthNames = monthNames.GetRange(index, 12 - index).AddRange(monthNames.GetRange(0, index);
There are many ways to do it, like others have shown.

Filter a Range of Dates with a String of Days

I'm working with a collection of DateTime with all dates from Date A to Date B.
I will be handed a string which looks like 1234567, 1 is sunday, 2 is tuesday, etc.
Now, imagine I want to filter my dates collection using a string with the above configuration and we get the string 1004007, meaning we will have to filter our DateTime collection to only have dates which occur on a sunday, on a wednesday and a saturday.
How can I read the whole string, figure out which days I will be filtering from and then dynamically filter my collection according to those days of the week?
Give this a shot:
List<DateTime> dates = ...;
string filter = "1004007";
List<DateTime> filteredDates = dates.Where(d =>
filter.Contains(((int)d.DayOfWeek + 1).ToString())).ToList();
Or, if you like, you can first construct a list of days that are your filter rather than just using the String.Contains function. If your list of dates is very large, doing this work up front could help performance:
List<DateTime> dates = ...;
string filter = "1004007";
var daysOfWeek = filter.Distinct().Where(c => c != '0')
.Select(c => (DayOfWeek)(int.Parse(c.ToString()) - 1))
List<DateTime> filteredDates = (from d in dates
join dw in daysOfWeek on d.DayOfWeek equals dw
select d).ToList();
Convert the day of the week to an integer, then a string, and use Contains to see if it is in your input string:
string days = "1004007";
var result = datetimes
.Where(dt => days.Contains(((int)dt.DayOfWeek + 1).ToString()));

Categories

Resources