CommunityToolkit / CommunityToolkit/dotnet

Allow retrieve init and end date of a week given a date

Open
#658 0 comments 0 reactions 0 assignees View on GitHub
feature request :mailbox_with_mail:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Overview

Hello, here is the first entry of my proposals for functionalities to import from my current CommonNET project to the Microsoft Community Toolkit, I hope you find it useful (and if not, nothing happens).

In case you need to take as a reference the start or end day of the week in the Gregorian calendar for a given date, also contemplating whether the start of the week is on Monday or Sunday.

This helps to determine ranges for a specific week, for example if you need to do specific logic on a calendar.

For example, I needed it to establish some business rules on a user, for a specific week given a date X.

In the case presented, I'm using DateTime, but of course can be also used from DateTimeOffset and/or DateOnly

### API breakdown

```csharp

namespace CommunityToolkit.Common;

public static class DateTimeExtensions
{
public static DateTime GetFirstWeekDate(this DateTime date, WeekCalendarType weekCalendarType = WeekCalendarType.MondayFirst)
{
//Logic
}

public static DateTime GetLastWeekDate(this DateTime date, WeekCalendarType weekCalendarType = WeekCalendarType.MondayFirst)
{
//Logic
}

}

```

### Usage example

```csharp

var givenDate = new DateTime(2021, 10, 18);

//By default in the proposal, starting on Monday
var initialDay = givenDate.GetFirstWeekDate();
var endDay = givenDate.GetLastWeekDate();

//In this case if week start on Sunday, however the default behaviour can be switched
var initialDay = givenDate.GetFirstWeekDate(WeekCalendarType.SundayFirst);
var endDay = givenDate.GetLastWeekDate(WeekCalendarType.SundayFirst);

```

Examples in current test case file [here](https://github.com/ChustaSoft/CommonNET/blob/main/ChustaSoft.Common.UnitTest/TestServices/DateTimeHelperUnitTest.cs)

### Breaking change?

No

### Alternatives

Add as part of the boilerplate code in requiring project.

No current SDK implementation AFAIK.

### Additional context

This is the first proposal of a series of implementations that I made during time in a common project used for the projects of my OpenSource organization, personal projects and projects of the company in which I work.

Currently, I would prefer to include what is practical in a repository with greater scope and practicality, and eliminate what is not necessary or could be improved, leaving the library in a state of maintenance without new developments.

Please refer to [this](https://github.com/ChustaSoft/commonnet) project

### Help us help you

Yes, I'd like to be assigned to work on this item

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.