25 lines
753 B
C#
25 lines
753 B
C#
namespace newcle.us.Utilities
|
|
{
|
|
internal class DateTimeExtensions
|
|
{
|
|
public static DateTime EndOfWeek(DateTime dt, DayOfWeek startOfWeek = DayOfWeek.Monday)
|
|
{
|
|
// Normalize to date (ignore time for week math)
|
|
var date = dt.Date;
|
|
|
|
// Compute offset from the configured startOfWeek
|
|
int diff = (7 + (date.DayOfWeek - startOfWeek)) % 7;
|
|
|
|
// Start of this week
|
|
var start = date.AddDays(-diff);
|
|
|
|
// End of this week: start + 6 days, then set to end-of-day
|
|
var endDate = start.AddDays(4);
|
|
|
|
// End-of-day with max precision (DateTime has 100-ns ticks)
|
|
return endDate.Date.AddDays(1).AddTicks(-1);
|
|
}
|
|
|
|
}
|
|
}
|