使用JavaScript获取最后3个月
我需要帮助获取过去3个月的javascript。我已经生成了一个excel表格,用户必须在其中填写月份列中的月份编号和年份列中的年份 我需要验证用户是否根据当前年份输入了正确的月份和年份组合 用户填写excel并将其上载到node js fastify服务器。之后,我需要验证月份和年份 例如:使用JavaScript获取最后3个月,javascript,node.js,Javascript,Node.js,我需要帮助获取过去3个月的javascript。我已经生成了一个excel表格,用户必须在其中填写月份列中的月份编号和年份列中的年份 我需要验证用户是否根据当前年份输入了正确的月份和年份组合 用户填写excel并将其上载到node js fastify服务器。之后,我需要验证月份和年份 例如: 如果当前年份为2018年,月份为1月1日。用户可以在一年内进入2017年,在一个月内进入10年 本年度为2019年,本月为10年。用户可以在一个月内输入8 请引导我。我怎样才能做到这一点 const mo
const months = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
const getLastThreeMonths = () => {
const now = new Date();
const previousMonths = [];
for (let i = 0; i < 3; i += 1) {
previousMonths.push(`${months[(now.getMonth() - i)]}`);
}
return previousMonths;
};
console.log(getLastThreeMonths());
const months=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
const getLastThreeMonths=()=>{
const now=新日期();
const-previousMonths=[];
对于(设i=0;i<3;i+=1){
push(${months[(now.getMonth()-i)]});
}
返回上个月;
};
log(getLastThreeMonths());
这里有一个工作代码示例。您可以根据需要更改内容
$(document).ready(function () {
var date = new Date();
var month = date.getMonth() + 1;
var year = date.getFullYear();
if (month == "1") {
year = date.getFullYear() - 1;
}
month = month - 2;
$("#month").on('change', function () {
if ($("#month").val() != month) { alert("not valid month"); }
});
$("#year").on('change', function () {
if ($("#year").val() != year) {alert("not valid year"); }
});
});
<input type="text" id="month" placeholder="put month here"/>
<input type="text" id="year" placeholder="put year here"/>
$(文档).ready(函数(){
变量日期=新日期();
var month=date.getMonth()+1;
var year=date.getFullYear();
如果(月份=“1”){
年份=日期。getFullYear()-1;
}
月份=第2个月;
$(“#月”)。关于('change',函数(){
如果($(“#月”).val()!=month){alert(“无效月”);}
});
$(“#年”)。关于('change',函数(){
如果($(“#年”).val()!=year){alert(“无效年”)}
});
});
我的答案:
const dateValidator = (month, year) => {
const allowedRange = [];
for (let i = 1; i <= 3; i += 1) {
const current = new Date();
current.setMonth(current.getMonth() - i + 1);
allowedRange.push({ month: current.getMonth() + 1, year: current.getFullYear() });
}
return allowedRange.find(ar => ar.month === month && ar.year === year);
};
const-dateValidator=(月、年)=>{
常量allowedRange=[];
for(设i=1;i ar.month==month&&ar.year==year);
};
到目前为止,您尝试了什么?你能分享一些代码吗?请检查。如我所述,如果当月为2019年1月,则他/她可以添加2018年10月。