24

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/

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 4
    Isn't the last day of the year always 31-12-[year]? – Ian Nov 06 '12 at 16:20
  • 2
    the last day of the year is always December 31. Why would you need to calculate that? – Jason Nov 06 '12 at 16:20
  • @Jason In case of it changes... – sp00m Nov 06 '12 at 16:21
  • Well, the world is ending in 2012, as we all know, so he needs to get the last day of the year from the Mayan calendar. – aquinas Nov 06 '12 at 16:22
  • You will find the answer [here][1] on the stackoverflow. [1]: http://stackoverflow.com/questions/222309/calculate-last-day-of-month-in-javascript – Rikki Nov 06 '12 at 16:23
  • @sp00m: We've pretty much got the [calendar figured out](http://en.wikipedia.org/wiki/Gregorian_calendar) at this point; I very much doubt that we'll change the length of December. – josh3736 Nov 06 '12 at 16:25
  • Lol thx, i forgot that 31 is the last day D: –  Nov 06 '12 at 16:26
  • hehe, check this: http://davidlongstreet.wordpress.com/2009/05/19/dont-drink-and-code/ – Remco Ros Nov 06 '12 at 16:30

6 Answers6

80

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

new Date(new Date().getFullYear(), 11, 31)
josh3736
  • 139,160
  • 33
  • 216
  • 263
8

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)
Mayerson
  • 101
  • 1
  • 8
2

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)
Abdul Qadir Memon
  • 950
  • 1
  • 12
  • 27
1
alert(new Date(2012, 12, 0));

will return

Mon Dec 31 00:00:00 CST 20102
Rikki
  • 3,338
  • 1
  • 22
  • 34
  • If you pass 11 it will return the Nov month. :) try it. It's the reason why I passed the zero value as the third parameter. Cheers – Rikki Nov 06 '12 at 16:45
1

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 );
}
josh3736
  • 139,160
  • 33
  • 216
  • 263
Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57
0

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

try:

var tmp = new Date(year, month+1, 1);
var days = new Date(tmp-1).getDate();
Remco Ros
  • 1,467
  • 15
  • 31