gpt4 book ai didi

.net - 如何在给定日期的一周内获得工作日?

转载 作者:行者123 更新时间:2023-12-04 02:30:30 30 4
gpt4 key购买 nike

这个问题可能不是最清楚的,但这是我需要的:

var date = new LocalDate(2020, 05, 05);

我需要给定日期所在一周的星期一和星期五的日期。

最佳答案

我建议将代码编写为日期调整器:

public static class DayOfWeekAdjusters
{
public static Func<LocalDate, LocalDate> ForIsoDayOfWeek(IsoDayOfWeek dayOfWeek) =>
date => WeekYearRules.Iso.GetLocalDate(
WeekYearRules.Iso.GetWeekYear(date),
WeekYearRules.Iso.GetWeekOfWeekYear(date),
dayOfWeek);
}

然后你可以使用:

private static readonly Func<LocalDate, LocalDate> MondayAdjuster =
DayOfWeekAdjusters.ForIsoDayOfWeek(IsoDayOfWeek.Monday);
private static readonly Func<LocalDate, LocalDate> FridayAdjuster =
DayOfWeekAdjusters.ForIsoDayOfWeek(IsoDayOfWeek.Friday);

...
var date = new LocalDate(2020, 05, 05);
var monday = date.With(MondayAdjuster);
var friday = date.With(FridayAdjuster);

您也可以将这些调节器与 LocalDateTimeOffsetDateTime 一起使用。

关于.net - 如何在给定日期的一周内获得工作日?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64467615/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com