Get the last day of the year in javascript

Today is 11/06/2012. I need to get 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 retrieves it, or should I complete the full implementation? If I need to implement this, can someone help me?

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

+8
javascript datetime
source share
5 answers

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

 new Date(new Date().getFullYear(), 11, 31) 
+32
source share

If one day the last day of the year changes, it may be useful to use a temporary date with the first day of the next month, and then return to the previous date.

 tmp_date = new Date(2012, 12, 1) last_day = new Date(tmp_date - 1) 
+5
source share
 alert(new Date(2012, 12, 0)); 

will return

 Mon Dec 31 00:00:00 CST 20102 
+1
source share

Will it do it for you? This will bring back the Whole batch for you.

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

I think OP wants to get the last day of the month.

to try:

 var tmp = new Date(year, month+1, 1); var days = new Date(tmp-1).getDate(); 
0
source share

All Articles