javascript 用javascript检索一年的最后一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13255114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:12:57  来源:igfitidea点击:

Retrieve the last day of the year in javascript

javascriptdatetime

提问by josh3736

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

今天是 06/11/2012。我需要使用 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/

我做了一个简单的小提琴,你可以在这里查看:http: //jsfiddle.net/EyzCD/

回答by josh3736

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

由于一年的最后一天总是 12 月 31 日,所以很简单:

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

回答by Mayerson

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)

回答by Rikki

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

will return

将返回

Mon Dec 31 00:00:00 CST 20102

回答by Magnus Karlsson

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 );
}

回答by Remco Ros

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

我认为 OP 想要获得本月的最后一天。

try:

尝试:

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

回答by Abdul Qadir Memon

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)