Reputation: 34
I need to get the current working week from our current day. Example:
22.11.2022(Tuesday) should return me the dates from 21.11.2022(Monday) - 26.11.2022(Saturday)
27.11.2022(Sunday) should return me the next week: 28.11.2022(Monday) - 03.12.2022(Saturday).
I found this stack overflow question, but in Java(Don't really understand, how to translate it to c#): How to find nearest week day for an arbitrary date?
Upvotes: 0
Views: 216
Reputation: 117029
This is all you need:
(DateTime start, DateTime end) GetWorkingWeek(DateTime fromDate)
{
var start = fromDate.Date.AddDays(1.0 - (int)fromDate.DayOfWeek);
var end = start.AddDays(5.0);
return (start: start, end: end);
}
The fromDate.Date
strips the time from the DateTime
. The DayOfWeek
is an enum made of the numbers from 0
for Sunday to 6
for Saturday, so casting the DayOfWeek
to an int
and subtracting that many days from fromDate.Date
brings the date back to Sunday. The 1
adds a day to Monday. Then adding 5
gets Saturday.
Now, from your sample input:
DateTime[] fromDates = new[]
{
new DateTime(2022, 11, 22),
new DateTime(2022, 11, 27),
};
(DateTime start, DateTime end)[] workingWeeks =
fromDates
.Select(GetWorkingWeek)
.ToArray();
That gives:
Upvotes: 1
Reputation: 1
For me this works pretty well. It gives a list of the working days of the week
public List<DateTime> GetWorkingDaysOfWeek(DateTime date)
{
List<DateTime> workingDays = new List<DateTime>();
date = date.AddDays(-(date.Date.DayOfWeek.GetHashCode() - 1));
for (int i = 0; i < 7; i++)
{
workingDays.Add(date.AddDays(i));
}
return workingDays;
}
Upvotes: 0
Reputation: 57
This might be an overkill. But I created a small simple self explanatory Console Program for your problem statement.
static void Main(string[] args)
{
Console.WriteLine("Enter the date for finding the List of WrokWeek in mm/dd/yyyy. If you do not eneter any date. Today's date will be taken by default");
string inputDate = Console.ReadLine();
DateTime parsedInputDateTime = DateTime.UtcNow;
DateTime.TryParse(inputDate, out parsedInputDateTime);
DateTime inputDateInDateTime = parsedInputDateTime != DateTime.MinValue ? parsedInputDateTime : DateTime.UtcNow;
// Now we find out the Day of the week from the input Date
var dayOfWeekForInputDate = inputDateInDateTime.DayOfWeek;
// Just find the Nearest Sunday (Day=0) in the past
int nearestSundayDiff = dayOfWeekForInputDate != DayOfWeek.Sunday ? dayOfWeekForInputDate - DayOfWeek.Sunday : 0;
// Now using the difference value we basically know how far we have to subtract to reach Sunday
// When we reach Sunday we add one more day to get Monday and rest is knida self-explanatory
DateTime nearestSunday = inputDateInDateTime.AddDays(-nearestSundayDiff);
DateTime currentDay = nearestSunday.AddDays(1);
// Just Loop through the days and print out the week dates from Monday to Saturday
while (currentDay.DayOfWeek != DayOfWeek.Sunday)
{
string s = currentDay.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(currentDay.DayOfWeek.ToString() + " : " + s);
currentDay = currentDay.AddDays(1);
}
Console.ReadKey();
}
This Console program takes in a string in mm/dd/yyyy format and parses it in the backend. If the format is incorrect or no date is entered it just takes the current UTC Date and Time.
Then we basically try to find the nearest Sunday as it's integral value is 0 in C#. Then we just loop through to the Saturday and print out the result. I would not suggest considering this code production worthy as I wrote it in 5 mins and can certainly think of atleast 3-4 things I can do to prevent errors etc. Feel free to comment out the mistakes.
This is the response when I entered 12/02/2022
Hopefully this answers your query. Have a great day
Upvotes: 0