Javascript如何从当前月份算起满两年?
我对从当前日期算起满两年有疑问。因此,我所做的就是使用新的日期函数获取当前月份,并使用for循环打印每个月份。但是,我真的无法让它发挥作用。。。。我将在下面发布我所做的代码。如果有人能告诉我这样做的逻辑或更好的方式,我将非常感激 例如:如果今天的当前日期是8月,它将从2020年8月9日存储到一个数组中。。。。。2020年12月、2021年1月和2022年8月的下一年Javascript如何从当前月份算起满两年?,javascript,html,css,reactjs,date,Javascript,Html,Css,Reactjs,Date,我对从当前日期算起满两年有疑问。因此,我所做的就是使用新的日期函数获取当前月份,并使用for循环打印每个月份。但是,我真的无法让它发挥作用。。。。我将在下面发布我所做的代码。如果有人能告诉我这样做的逻辑或更好的方式,我将非常感激 例如:如果今天的当前日期是8月,它将从2020年8月9日存储到一个数组中。。。。。2020年12月、2021年1月和2022年8月的下一年 var d = new Date(); var year = d.getFullYear(); var dateStr; va
var d = new Date();
var year = d.getFullYear();
var dateStr;
var currentYear;
var storeMonthYear = [];
for(var i = 1; i <= 24; i++){
dateStr = d.getMonth() + i
currentYear = year;
if(dateStr > "12"){
dateStr = dateStr - 12
// currentYear = year;
// if(currentYear){
// }
storeMonthYear[i] = dateStr + "/" + (currentYear + 1);
}
else if(dateStr > "24"){
storeMonthYear[i] = dateStr + "/" + (currentYear + 1);
}
else{
storeMonthYear[i] = dateStr + "/" + currentYear;
}
storeMonthYear[i] = d.getMonth() + i
}
export const settlementPeriod = [
{
MonthYearFirstRow1: storeMonthYear[1],
MonthYearFirstRow2: storeMonthYear[2],
MonthYearFirstRow3: storeMonthYear[3],
MonthYearFirstRow4: storeMonthYear[4],
MonthYearFirstRow5: storeMonthYear[5],
MonthYearFirstRow6: storeMonthYear[6],
MonthYearFirstRow7: storeMonthYear[7],
MonthYearFirstRow8: storeMonthYear[8],
MonthYearFirstRow9: storeMonthYear[9],
MonthYearFirstRow10: storeMonthYear[10],
MonthYearFirstRow11: storeMonthYear[11],
MonthYearFirstRow12: storeMonthYear[12],
MonthYearSecondRow13: storeMonthYear[13],
MonthYearSecondRow14: storeMonthYear[14],
MonthYearSecondRow15: storeMonthYear[15],
MonthYearSecondRow16: storeMonthYear[16],
MonthYearSecondRow17: storeMonthYear[17],
MonthYearSecondRow18: storeMonthYear[18],
MonthYearSecondRow19: storeMonthYear[19],
MonthYearSecondRow20: storeMonthYear[20],
MonthYearSecondRow21: storeMonthYear[21],
MonthYearSecondRow22: storeMonthYear[22],
MonthYearSecondRow23: storeMonthYear[23],
MonthYearSecondRow24: storeMonthYear[24]
},
];
var d=新日期();
var year=d.getFullYear();
var-dateStr;
var本年度;
var storeMonthYear=[];
对于(var i=1;i“12”){
dateStr=dateStr-12
//当前年份=年;
//如果(当前年度){
// }
storeMonthYear[i]=dateStr+“/”+(当前年份+1);
}
如果出现其他情况(dateStr>“24”){
storeMonthYear[i]=dateStr+“/”+(当前年份+1);
}
否则{
storeMonthYear[i]=dateStr+“/”+当前年份;
}
storeMonthYear[i]=d.getMonth()+i
}
导出常量结算周期=[
{
MonthYearFirstRow1:storeMonthYear[1],
MonthYearFirstRow2:storeMonthYear[2],
MonthYearFirstRow3:storeMonthYear[3],
MonthYearFirstRow4:storeMonthYear[4],
MonthYearFirstRow5:storeMonthYear[5],
MonthYearFirstRow6:storeMonthYear[6],
MonthYearFirstRow7:storeMonthYear[7],
MonthYearFirstRow8:storeMonthYear[8],
MonthYearFirstRow9:storeMonthYear[9],
MonthYearFirstRow10:storeMonthYear[10],
MonthYearFirstRow11:storeMonthYear[11],
MonthYearFirstRow12:storeMonthYear[12],
MonthYearSecondRow13:storeMonthYear[13],
MonthYearSecondRow14:storeMonthYear[14],
MonthYearSecondRow15:storeMonthYear[15],
MonthYearSecondRow16:storeMonthYear[16],
MonthYearSecondRow17:storeMonthYear[17],
MonthYearSecondRow18:storeMonthYear[18],
MonthYearSecondRow19:storeMonthYear[19],
MonthYearSecondRow20:storeMonthYear[20],
MonthYearSecondRow21:storeMonthYear[21],
MonthYearSecondRow22:storeMonthYear[22],
MonthYearSecondRow23:storeMonthYear[23],
MonthYearSecondRow24:storeMonthYear[24]
},
];
从今天开始创建日期,获取月份和年份。从0到24迭代到24个月。如果月份为12,则将月份设置为0,并递增年份。推送新的日期字符串。增加下一步的月份。注意:Beacsue JS从0-11开始计算月份,您必须为该月份的日期字符串1添加月份,并将年份更改为12,而不是13
let date=新日期();
让year=date.getFullYear();
let month=date.getMonth();
设res=[];
对于(i=0;i这里,从开始月份到最后一个月,您会得到一个字符串数组,如“8/2020”、“9/2020”等,包括这两个字符串(总共25个月)。
如果您不想包括上个月,只需从for循环条件中删除+1即可
let currentDate = new Date();
let settlementPeriod = [];
let numberOfMonths = 24;
for(let i=0;i<numberOfMonths+1;i++){
settlementPeriod.push(currentDate.getMonth()+1+"/"+currentDate.getFullYear()); //We add current date objects attributes to the array
currentDate = new Date(currentDate.setMonth(currentDate.getMonth()+1)); //Every time we add one month to it
}
console.log(settlementPeriod);
让currentDate=新日期();
让结算周期=[];
设numberOfMonths=24;
对于(设i=0;i),在代码示例中有两个突出的方面:
- 您正在比较字符串和数字(例如,
dateStr>“12”
)。这会导致一些奇怪的错误,是JS最容易被误用的“功能”之一。请尽可能避免使用
- 从现在起到12个月时,而不是到下一个1月时,您将增加年数
- 您正在使用以下行覆盖字符串
storeMonthYear[i]=d.getMonth()+i
,因此您的数组是一组数字,而不是您期望的日期字符串
下面是一个代码示例,我认为它符合您的期望:
函数下个24个月(){
const today=新日期()
让year=today.getFullYear()
让monthIndex=today.getMonth()
让日期=[]
while(dates.length<24){
dates.push(`monthIndex+1}/${year}`)
//增加一个月,如果我们已经过了12月,
//我们需要提前一年,推迟一个月
//到一月
如果(++monthIndex>11){
monthIndex=0
年++
}
}
返回日期
}
一般来说,当你处理日期时,你最好使用类似于库的程序——日期/时间是最难的编程概念之一。虽然@Ognjen的答案是正确的,但如果你的日期永远不会逃逸它的功能,这也有点浪费
您不需要每次都有新的日期:
function getPeriods(firstMonth, numPers){
var d = new Date(firstMonth.getTime()); // clone the start to leave firstMonth alone
d.setDate(1); // fix after @RobG
var pers = [];
var m;
for(var i = 0; i< numPers; i++){
m = d.getMonth();
pers.push(`${m+ 1}/${d.getFullYear()}`)
d.setMonth(m + 1); // JS dates automatically roll over. You can do this with d.setDate() as well and when you assign 28, 29, 31 or 32 the month and year roll over automatically
}
return pers;
}
function getPeriods(第一个月,numPers){
var d=new Date(firstMonth.getTime());//克隆开始以保留firstMonth
d、 setDate(1);//在@RobG之后修复
var pers=[];
var-m;
对于(var i=0;i
在数组中,您需要这样的结构[M/YYYY,M/YYYY,…]还有没有日期对象?非常感谢!我将使用此方法查看itTry将1个月添加到1月31日。很高兴为您提供帮助。如果您对此感到满意,您可以接受我的答案。Ognjen的答案不正确。如果代码在1月31日运行,则添加1个月将在闰年返回2个月,在其他年份返回3个月,跳过2月里。