How to get the first day and the last day of the current year in c#
How do I get the first day and the last day of the current year in c#
This?
int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31);
Try this:
var firstDay = new DateTime(DateTime.Now.Year, 1, 1);
var lastDay = new DateTime(DateTime.Now.Year, 12, 31);
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 :)
Why not getting the first day of the next calendar year (month 1, day 1) and subtract one day.