How do I get the first day and the last day of the current year in c#
How to 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);
This will skip the last day since it will generate 31st of December 00:00 instead of 23:59 –
Interpellation
The question was about day, not datetime –
Staid
True but most people don't make a difference and make this mistake unknowingly. That's why I prefer to leave this comment here. People who need just the date will just ignore my comment. –
Interpellation
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 :)
Just a side note: if you're using this with bridge.net to translate to js use AddSeconds since "ticks" is too small for js. –
Interpellation
The answers provide a
DateTime
value on the last day of the year, as requested in the question. The question doesn't mention anything about the last instant of the last day. Note that if "last instant" is going to be used in some kind of condition, it's generally better to give an exclusive upper bound (the start of the following year) so that precision doesn't matter. (It's not great if you have to interface with something that does only have precision of 1 second, and you end up missing out "all but one tick of the last second".) –
Bromeosin If you're treating the values as days and you want to include the last day, I would suggest using date comparisons. There's no need to find "the last representable date and time" and then compare using that, risking problems if you need to propagate that value to systems with more precision. If you stick to consistent precision, and perform comparisons using that precision (date in this case) there's no problem. –
Bromeosin
True, thanks for the advice, it's certainly very useful. But 99% of the people who need the end of the year skip all the data created during the last day exactly because they don't get the last instant of the day. I've seen this bug more than I can count. Also if you're going to trim the keys to be used in the UI it would really suck if the upper bound is next year. Sometimes tiny optimizations are not worth it. –
Interpellation
I don't understand your UI perspective - but I would say that thinking in terms of exclusive upper bounds (and inclusive lower bounds) is generally a good thing to get used to. It typically leads to much simpler code and more correct code. –
Bromeosin
Thanks for the advice, I'll be sure to keep that in mind for the future. –
Interpellation
Why not getting the first day of the next calendar year (month 1, day 1) and subtract one day.
If you want to make sure to include Dec. 31st until midnight, you should include the time:
DateTime firstDay = new DateTime(DateTime.Now.Year, 1, 1, 0, 0, 0);
DateTime lastDay = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59, 999);
© 2022 - 2024 — McMap. All rights reserved.