How to get the first day and the last day of the current year in c#
Asked Answered
H

5

74

How do I get the first day and the last day of the current year in c#

Hornmad answered 6/1, 2013 at 10:9 Comment(0)
S
159

This?

int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31);
Staid answered 6/1, 2013 at 10:14 Comment(3)
This will skip the last day since it will generate 31st of December 00:00 instead of 23:59Interpellation
The question was about day, not datetimeStaid
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
M
36

Try this:

var firstDay = new DateTime(DateTime.Now.Year, 1, 1);
var lastDay = new DateTime(DateTime.Now.Year, 12, 31);
Masorete answered 6/1, 2013 at 10:15 Comment(0)
I
15

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 :)

Interpellation answered 28/8, 2019 at 14:58 Comment(6)
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
S
0

Why not getting the first day of the next calendar year (month 1, day 1) and subtract one day.

Soapberry answered 15/7, 2015 at 15:52 Comment(0)
C
0

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);
Curative answered 9/1 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.