C# 中一年的最后一个星期日
如何在 C# 中获取一年中最后一个星期日的日期?公历日历类会有帮助吗?
How can I get date of last sunday of a year in C#?? Will gregoriancalendar class be of help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
抱歉,我没有 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.
这取决于...您想要一年中的最后一天是星期日,还是一年中最后一周的星期日?根据您所在国家/地区确定周数的方式,一年中的最后一周可能会持续到下一年的六天。
如果你想要第一个,你可以从 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.我不知道是否有方法,但一个简单的方法是检查 12 月的最后 7 天,从第 31 天开始倒计时。
更新:实际上,如果一周中的天数如下所示:
那么
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:
then
不是最好的解决方案,但应该有效:
Not the nicest solution but should work:
我认为您可以使用 DayOfWeek 枚举的整数表示形式:
I think you can use the integer representation of the
DayOfWeek
enumeration for this:由于 DayOfWeek 的枚举开始于0 表示周日,这应该有效:
As the enumeration of DayOfWeek starts at 0 for Sunday, this should work:
您可以了解 12 月 31 日是星期几。然后(如果不是星期日)计算回来......
You can find out what day of the week december 31 is. Then (if not a sunday) calculate back ...