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

C#Date

C# Problem Overview


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

C# Solutions


Solution 1 - C#

This?

int year = DateTime.Now.Year;
DateTime firstDay = new DateTime(year, 1, 1);
DateTime lastDay = new DateTime(year, 12, 31);

Solution 2 - C#

Try this:

var firstDay = new DateTime(DateTime.Now.Year, 1, 1);
var lastDay = new DateTime(DateTime.Now.Year, 12, 31);

Solution 3 - C#

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

Solution 4 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1930115View Question on Stackoverflow
Solution 1 - C#Hamlet HakobyanView Answer on Stackoverflow
Solution 2 - C#boindiilView Answer on Stackoverflow
Solution 3 - C#Bojidar StanchevView Answer on Stackoverflow
Solution 4 - C#Liron HarelView Answer on Stackoverflow