码迷,mamicode.com
首页 > Web开发 > 详细

js【输入一个日期】返回【当前12个月每月最后一天】

时间:2016-05-21 17:36:01      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
//获得某月的最后一天
function getLastDay(year, month) {
var new_year = year; //取当前的年份
var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
if (month > 12) {
new_month -= 12; //月份减
new_year++; //年份增
}
var new_date = new Date(new_year, new_month, 1); //取当年当月中的第一天
return new Date(new_date.getTime() - 1000 * 60 * 60 * 24).Format(‘yyyy-MM-dd‘);//获取当月最后一天日期
}
function getLastDayForN(y, m, n) {
if (n > 0) {
var yy = m === 1 ? y - 1 : y;
var mm = m - 1 || 12;
getLastDayForN(yy, mm, n - 1);
}
var currentDate = getLastDay(y, m);
console.log(currentDate);
}
getLastDayForN(2016,5,12);

js【输入一个日期】返回【当前12个月每月最后一天】

标签:

原文地址:http://www.cnblogs.com/wu-peng/p/5514928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!