Retrieve the last day of the year in javascript

Today it’s 06/11/2012.
I need retrieve the last day of the current year using javascript.

function getLastDayOfYear(date)
{
    var x = document.getElementById("demo");

    var year = date.getFullYear();
    var month = date.getMonth();
    var day = 0; // ?????

    x.innerHTML = day + "-" + month + "-" + year;
}  

Is there any function that retrieve it done, or must i do a full implementation?
If i need to implement this, could anyone help me out ?

I made a simple fiddle you can check here: http://jsfiddle.net/EyzCD/

Since the last day of a year is always December 31, it’s easy:

new Date(new Date().getFullYear(), 11, 31)

In case that someday the last day of the year changes, could be useful to use a temporary date with the first day on the next month and then return to the previous date

tmp_date = new Date(2012, 12, 1)
last_day = new Date(tmp_date - 1)

alert(new Date(2012, 12, 0));

will return

Mon Dec 31 00:00:00 CST 20102

Will this do it for you?
This will return the Whole lot for you to pick from.

function LastDayOfMonth(Year, Month) {
   return new Date( (new Date(Year, Month,1))-1 );
}

This will return Last day of Year

var actualDate = new Date()
var eoYear = new Date(actualDate.getFullYear(),12,0)

In case if you need to return Last second of the year

var actualDate = new Date()
var eoYear = new Date(actualDate.getFullYear(),12,0,23,59,59)


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts