C# 中一年的最后一个星期日

发布于 2024-08-04 20:47:49 字数 42 浏览 4 评论 0原文

如何在 C# 中获取一年中最后一个星期日的日期?公历日历类会有帮助吗?

How can I get date of last sunday of a year in C#?? Will gregoriancalendar class be of help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

2024-08-11 20:47:50

抱歉,我没有 C# 版本,如果您看到此处 VB 版本。我怀疑你能够转换它。

Sorry but I don't have a C# version be if you see here for VB version. I suspect you'll be able to convert it.

仄言 2024-08-11 20:47:50

这取决于...您想要一年中的最后一天是星期日,还是一年中最后一周的星期日?根据您所在国家/地区确定周数的方式,一年中的最后一周可能会持续到下一年的六天。

如果你想要第一个,你可以从 12 月 31 日开始,然后返回,直到找到周日。

如果您想要第二个,则必须使用 System.Globalizartion.Calendar.GetWeekOfYear 方法来找出下一年第一周的开始位置,并获取该日期的前一天。

That depends... Do you want the last day of the year that is a sunday, or the sunday of the last week of the year? Depending on how weeks are determined in your country, the last week of the year may go up to six days into the next year.

If you want the first, you can just start at the 31st of december and go back until you find a sunday.

If you want the second, you will have to use the System.Globalizartion.Calendar.GetWeekOfYear method to find out where the first week of the next year starts, and take the day before that.

人疚 2024-08-11 20:47:49

我不知道是否有方法,但一个简单的方法是检查 12 月的最后 7 天,从第 31 天开始倒计时。

更新:实际上,如果一周中的天数如下所示:

0 Sun, 1 Mon, ... 6 Sat

那么

lastSunday = 31 - DayOfWeek(31, 12, 2009) // pseudocode

I don't know if there is a method for that but a simple way is to check the last 7 days of December, starting from 31st day and counting down.

Update: actually if the days of week are numbered like this:

0 Sun, 1 Mon, ... 6 Sat

then

lastSunday = 31 - DayOfWeek(31, 12, 2009) // pseudocode
酒废 2024-08-11 20:47:49

不是最好的解决方案,但应该有效:

int year = 2009;
DateTime lastSunday = new DateTime(year, 12, 31);
while (lastSunday.DayOfWeek != DayOfWeek.Sunday)
{
    lastSunday = lastSunday.AddDays(-1);
}

Not the nicest solution but should work:

int year = 2009;
DateTime lastSunday = new DateTime(year, 12, 31);
while (lastSunday.DayOfWeek != DayOfWeek.Sunday)
{
    lastSunday = lastSunday.AddDays(-1);
}
忘东忘西忘不掉你 2024-08-11 20:47:49

我认为您可以使用 DayOfWeek 枚举的整数表示形式:

DateTime endOfYear = new DateTime(DateTime.Now.Year, 12, 31);
DateTime lastSunday = endOfYear.AddDays(-(int)endOfYear.DayOfWeek);

I think you can use the integer representation of the DayOfWeek enumeration for this:

DateTime endOfYear = new DateTime(DateTime.Now.Year, 12, 31);
DateTime lastSunday = endOfYear.AddDays(-(int)endOfYear.DayOfWeek);
甜宝宝 2024-08-11 20:47:49

由于 DayOfWeek 的枚举开始于0 表示周日,这应该有效:

    int Year = 2009;
    DateTime endOfYear = new DateTime(Year, 12, 31);
    DateTime sunday = endOfYear.AddDays(-(int)endOfYear.DayOfWeek); 

As the enumeration of DayOfWeek starts at 0 for Sunday, this should work:

    int Year = 2009;
    DateTime endOfYear = new DateTime(Year, 12, 31);
    DateTime sunday = endOfYear.AddDays(-(int)endOfYear.DayOfWeek); 
嗳卜坏 2024-08-11 20:47:49

您可以了解 12 月 31 日是星期几。然后(如果不是星期日)计算回来......

for (int y=2009; y<=2018; y++)
{
  DateTime dt = new DateTime(y, 12, 31); // last day of year
  dt = dt.AddDays(-(int)dt.DayOfweek); // sun == 0, so then no change
  Console.WriteLine(dt.ToString("dddd dd-MM-yyyy"));
}

You can find out what day of the week december 31 is. Then (if not a sunday) calculate back ...

for (int y=2009; y<=2018; y++)
{
  DateTime dt = new DateTime(y, 12, 31); // last day of year
  dt = dt.AddDays(-(int)dt.DayOfweek); // sun == 0, so then no change
  Console.WriteLine(dt.ToString("dddd dd-MM-yyyy"));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文