如何在c#中获取当前年份的第一天和最后一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14181113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the first day and the last day of the current year in c#
提问by user1930115
How do I get the first day and the last day of the current year in c#
如何在c#中获得当前年份的第一天和最后一天
采纳答案by Hamlet Hakobyan
This?
这个?
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year , 1, 1);
DateTime lastDay = new DateTime(year , 12, 31);
回答by boindiil
Try this:
尝试这个:
var firstDay = new DateTime(DateTime.Now.Year, 1, 1);
var lastDay = new DateTime(DateTime.Now.Year, 12, 31);
回答by Idan Shechter
Why not getting the first day of the next calendar year (month 1, day 1) and subtract one day.
为什么不获取下一个日历年的第一天(第 1 个月,第 1 天)并减去一天。
回答by Bojidar Stanchev
None of the answers here actually account for the last day. In my opinion, the correct way to do this would be:
这里的答案实际上都没有考虑到最后一天。在我看来,这样做的正确方法是:
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year , 1, 1);
DateTime lastDay = firstDay.AddYears(1).AddTicks(-1)
Hope this would be valuable to someone :)
希望这对某人有价值:)