获取指定日期的星期日

时间:2016-11-20 07:31:01

标签: c# datetime

如何获取指定周的星期日日期?

示例,如果我的日期时间包含“11/19/2020”的日期,我怎样才能获得11/19周的星期日?该值为11/13。

2 个答案:

答案 0 :(得分:2)

这是您所需要的

DateTime d = new DateTime(2020,11,19);           
int diff = d.DayOfWeek - DayOfWeek.Sunday;             
DateTime lastSunday = d.AddDays(-diff);           
Console.WriteLine(lastSunday);

答案 1 :(得分:0)

new DateTime(2020,11,19).AddDays(-(new DateTime(2020,11,19).DayOfWeek - DayOfWeek.Sunday))