Find the last day of the year

27.9k views Asked by At

I am doing a task in which it is required to find the last day of the year by given current date.

Like if its 06/Jan/2013 is given to it and it will automatically return the end day of the year. like 31/Dec/2013

I am using many ways like adding the remaining days of the year, but when it is Leap year then miss matches occurs, can any body send me precise solution or guied me how to.

Basically I wanted to jump over the last date of the year.This was tentively for C#

6

There are 6 answers

0
dcastro On

Why not something simple like this?

var date = new DateTime(2013, 1,6);
var lastDayOfTheYear = new DateTime(date.Year, 12, 31);
0
Michel Keijzers On

I would copy the date, change the day to 31, the month to December and you have the last day.

0
coolprarun On

You can try this one.. For getting First day and last day..

int year = DateTime.Now.Year;
DateTime firstDayyear = new DateTime(year , 1, 1);
DateTime lastDayyear = new DateTime(year , 12, 31);
0
Jeroen van Langen On

I think you mean Day of Week?

new DateTime(DateTime.Now.Year, 12, 31).DayOfWeek

or more readable:

DateTime lastDateOfYear = new DateTime(DateTime.Now.Year, 12, 31);
DayOfWeek dayOfWeek = lastDateOfYear.DayOfWeek;

More about DayOfWeek enum here: http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx

Or did you mean how many days left till next year.

DateTime lastDateOfYear = new DateTime(DateTime.Now.Year, 12, 31);
var daysTillNextYear = (lastDateOfYear - DateTime.Now).TotalDays;
0
DareDevil On

Here is the simple solution, I have searched many but after many tries, i did with this.

DateTime LastDayOfYear(DateTime date)
    {           
        DateTime newdate = new DateTime(date.Year + 1, 1, 1);
        //Substract one year
        return newdate.AddDays(-1);
    }
//Here calling the function 
DateTime current_time = LastDayOfYear(DateTime.Now); // 05,Sep ,2013
// It returned 31, Dec, 2013
0
Malcor On

To get the last day just use this:

string lastdayofyear = new DateTime(DateTime.Now.Year, 12,31).DayOfWeek.ToString();