I'm trying to force the date selected in a date input to the Sunday of the week of the date actually selected by the user. So if they pick Wednesday, it will actually change to the Sunday of the same week.
I've managed to do this but when I reselect a date in the same week again it doesn't work. As an example, when the user selects Wednesday, it successfully selects Sunday. But then when they go and select Tuesday instead, it keeps the displayed value as Tuesday instead of changing it to Sunday (which is what I'd want it to do)
Here's some example code
<input type="date" value="@overrideStart.ToString("yyyy-MM-dd")" @onchange="@SelectStartOfWeek" />
@code {
private DateTime overrideStart = DateTime.Today;
protected override async Task OnInitializedAsync()
{
overrideStart = GetStartOfWeek(overrideStart)
}
public async Task SelectStartOfWeek(ChangeEventArgs args)
{
var value = args.Value.ToString();
overrideStart = value == string.Empty ? DateTime.Today : DateTime.Parse(value);
overrideStart = await GetStartOfWeek(overrideStart);
}
private async Task<DateTime> GetStartOfWeek(DateTime date)
{
return date.AddDays(-(int)date.DayOfWeek);
}
}
Obviously the issue is because it recognizes that overrideStart ends up with the same value after SelectStartOfWeek finishes. I've tried StateHasChanged() but it doesn't seem to have any effect.