我有一个报告,用户只允许选择过去3个月的选项,如下所示:
我想编写一个javaScript函数来实现这个目的。
我尝试的是:
function getLastMonths(n) {
var months = new Array();
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var i = 0;
do {
months.push(year + (month > 9 ? "" : "0") + month);
if(month == 1) {
month = 12;
year--;
} else {
month--;
}
i++;
} while(i < n);
return months;
}
然后我通过传递参数来调用函数:
document.write(getLastMonths(4));
打印:
201704,201703,201702,201701
这不是我要求的模式。
有人可以帮助我吗?
答案 0 :(得分:2)
你可以这样做。只需继续从当前日期减去one month
function getLastMonths(n) {
var m =['January','February','March','April','May','June','July','August','September','October','November','December'];
var last_n_months =[]
var d = new Date()
for(var i=0;i<n;i++){
last_n_months[i] = m[d.getMonth()]+ ' - ' + d.getFullYear().toString()
d.setMonth(d.getMonth()-1)
}
return last_n_months
}
console.log(getLastMonths(3))
&#13;
答案 1 :(得分:1)
看看这里。由于 for loop
,它有点短
function getLast3Months() {
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var today = new Date();
var last3Months = []
for (i = 0; i < 3; i++) {
last3Months.push(monthNames[(today.getMonth() - i)] + ' - ' +today.getFullYear() );
}
return last3Months;
}
document.write(getLast3Months());
答案 2 :(得分:0)
试试这个。将月份名称添加到某些array
中,然后找到索引
function getLastMonths(n) {
var m =['','Jan','Feb','Mar','Aprl','May','Jun','July','Aug','Sep','Oct','Nov','Dec'];
var months = new Array();
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth()+1;
var i = 0;
do {
months.push( m[parseInt((month > 9 ? "" : "0") + month)]+'-'+year);
if (month == 1) {
month = 12;
year--;
} else {
month--;
}
i++;
} while (i < n);
return months;
}
document.write(getLastMonths(4));
答案 3 :(得分:0)
由于内部JS名称不公开,您必须手动将号码转换为名称。
请参阅下面的工作示例。这将完全输出您在下拉列表中显示月份的方式。
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
function getLastMonths(n) {
var months = new Array();
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth() + 1;
var i = 0;
do {
months.push(monthNames[month] + ' - ' + year);
if(month == 1) {
month = 12;
year--;
} else {
month--;
}
i++;
} while(i < n);
return months;
}
document.write(getLastMonths(4));
答案 4 :(得分:0)
如何使用moment.js?
var getLastMonths = function(n) {
var arr = new Array();
arr.push(moment().format('YYYY MM'));
for(var i=1; i< 3; i++){
arr.push(moment().add(i*-1, 'Month').format('YYYY MM'));
}
return arr;
}
var appendOptions = function(arr) {
var html = '';
for(var i=0; i<arr.length; i++) {
html += '<option value="' + arr[i] + '">' + arr[i] + '</option>'
}
document.getElementById('select-months').innerHTML = html;
}
var months = getLastMonths(4);
appendOptions(months);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<select id="select-months"></select>
答案 5 :(得分:0)
我已经修改了先前的代码并给出了一个示例: 小提琴:https://jsfiddle.net/asutosh/rogpxwah/
function getLast3Months() {
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var today = new Date(2019,3,12);
var month=today.getMonth();
var last3Months = []
for (i = 0; i < 3; i++) {
var newMonth=(today.getMonth() - i);
var newYear=today.getFullYear();
monthInWords=monthNames[newMonth];
if(newMonth<0){
newYear=today.getFullYear() - 1;
monthInWords=monthNames[12+newMonth];
}
var newDate= new Date(today.getDate(), newMonth, newYear);
last3Months.push(monthInWords + ' - ' +newYear );
}
return last3Months;
}
window.document.write(getLast3Months());
答案 6 :(得分:0)
以下是一个 Node 12+,ES2020 Generator 方法,代码少于 ~5 个节点...
function* lastMonths(start = 0, end = 4, step = 1, D = new Date()) {
const o = new Intl.DateTimeFormat("en-US" , { month: "long", year: "numeric" })
for (let i = start; i < end; i += step) {
const past = new Date(D)
void past.setMonth(D.getMonth() - i)
const localeStr = o.format(past).replace(' ', ' - ')
yield localeStr
}
return D
}
console.log([...lastMonths()])
[“7 月 - 2021 年”, “6 月 - 2021 年”, “5 月 - 2021 年”, “4 月 - 2021 年”]
来源: