Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

c#

People also ask

How do you find the first and last day of the current month?

To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates. Copied! const now = new Date(); const firstDay = new Date(now. getFullYear(), now.

How do I get the first day of the last month in C#?

AddMonths(-1); var last = month. AddDays(-1);


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.