I need two or one (out) C# method that will take any datetime and return the start date of year and end date of year for that year.
Asked
Active
Viewed 3.2k times
22
-
1What do you mean by start date of year? It will always be Jan 1 and Dec 31 right? – jfs May 23 '10 at 10:06
-
1The start date is Jan 1 and the end date is Dec 31. Am I missing something? – Marcelo Cantos May 23 '10 at 10:07
-
4I'm stupid; I guess (my brain has a corrupt stack) – abmv May 23 '10 at 10:08
-
2+1 You might feel stupid - but you're not. You're even getting points! – Sandor Drieënhuizen May 23 '10 at 10:13
2 Answers
46
void Dates(DateTime d, out DateTime b, out DateTime e)
{
b = new DateTime(d.Year, 1, 1);
e = new DateTime(d.Year, 12, 31);
}

nothrow
- 15,882
- 9
- 57
- 104
-
7For 6 years I had typo in the answer, got 20 points, and nobody noticed that. Nice :-) – nothrow May 05 '16 at 14:58
-
2also this will not include 12/31 if the time is important, than should be e = ...AddTicks(-1).AddDays(1) – Arsen Mkrtchyan Apr 03 '19 at 13:21
9
DateTime startDate = new DateTime(year, 1, 1);
DateTime endDate = new DateTime(year, 12, 31);

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928