动态地我想使用Jquery生成最近六个月 Demo here
var str = "";
var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
for ( var i = 5; i >= 0; i--) {
var now = new Date();
var date = new Date(now.setMonth(now.getMonth() - i));
var datex = ("0" + date.getDate()).slice(-2) + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + date.getFullYear();
str += "Date :"+datex+" | Month : " + monthNames[date.getMonth()] + "-" + date.getFullYear()+"\n";
}
上述代码在一个月的当前日期小于30日时工作正常,如果当前日期为31则上述代码生成具有不相关日期的重复月份。
任何帮助将不胜感激。谢谢。
答案 0 :(得分:0)
这是从当月开始的6个月内的最后一次粘贴
var str = "<option value=''>---Select Month---</option>";
var monthNames = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var varDate = new Date();
var month = varDate.getMonth()+1;
var currentDay = varDate.getDate();
for ( var i = 5; i >= 0; i--) {
var now = new Date();
now.setDate(1);
var date = new Date(now.setMonth(now.getMonth() - i));
var datex = ("0" + date.getDate()).slice(-2) + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + date.getFullYear();
str += "<option value='"+datex+"'>" + monthNames[date.getMonth()] + "-" + date.getFullYear() + "</option>";
}
$(".monthlyErrorReports").html(str);
这是针对当前和上个月的天数/日期
var fullMonthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var dateObject = new Date();
var currentMonth = dateObject.getMonth();
var strDateList = "<option value=''></option>"+"<optgroup label='--"+fullMonthNames[currentMonth]+"--'>";
var noOfDatesInMonth = daysInMonth(currentMonth,dateObject.getFullYear());
var varNoOfDaysFlag = noOfDatesInMonth;
var varItterationBreakFlag = true;
var varNoOfItteCount = 0;
var varTotalDaysCount = noOfDatesInMonth;
var currentDate = ("0" + dateObject.getDate()).slice(-2) + "-" + ("0" + (currentMonth+1)).slice(-2) + "-" + dateObject.getFullYear();
var previousDay = new Date(new Date().setDate(new Date().getDate() - 1));
previousDay = ("0" + previousDay.getDate()).slice(-2) + "-" + ("0" + (previousDay.getMonth()+1)).slice(-2) + "-" + previousDay.getFullYear();
for(var i = 1; i <= noOfDatesInMonth ; i++){
var dropdownValue = ("0" + i).slice(-2) + "-" + ("0" + (currentMonth+1)).slice(-2) + "-" + dateObject.getFullYear();
strDateList += "<option value='"+dropdownValue+"'>" +("0" + i).slice(-2)+"-"+ monthNames[currentMonth] + "-" + dateObject.getFullYear() + "</option>";
++varNoOfItteCount;
if(varNoOfDaysFlag == i && varItterationBreakFlag){
dateObject = new Date(dateObject.setMonth(dateObject.getMonth() - 1));
currentMonth = (dateObject.getMonth());
noOfDatesInMonth = daysInMonth(currentMonth,dateObject.getFullYear());
i = 0;
varItterationBreakFlag = false;
varTotalDaysCount += noOfDatesInMonth;
strDateList += "</optgroup><optgroup label='--"+fullMonthNames[currentMonth]+"--'>";
}
if(varTotalDaysCount == varNoOfItteCount){
noOfDatesInMonth = 0;
}
}
strDateList += "</optgroup>";
$(".NoOfDateList").html(strDateList);