如何获取从过去日期到现在的月份和年份JavaScript
How to get the month and year from the past date untill now JavaScript
我正在尝试找出从过去到现在的年月组合。
所以可以说,过去的日期是
const date = "December 2019"
那我需要一个数组
const arrayOfMonths = [ "April 2020", "March 2020", "February 2020", "January 2020", "December 2019"]
您可以使用 JavaScript Date 对象和 while 循环。
只需从当前月份减去一个月,就可以得到您想要的日期。
像这样的东西应该有用。
https://jsfiddle.net/wsf61jpk/5/
var date="December 2018";
var result=[];
//set both start and end date to first date of the month
const end_date = new Date(date.replace(" ", " ,1 "));
const start_date = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
while(end_date<=start_date){
result.push(start_date.toLocaleString('default', { month: 'long' , year: 'numeric'}));
start_date.setMonth(start_date.getMonth() - 1);
}
console.log(result);
我是利用 javascript 日期函数
做到的
var oldDate = "December 2019";
var old = new Date(Date.parse(oldDate));
var today = new Date();
var yearsCount= today.getFullYear() - old.getFullYear();
var monthsArr = [];
for(var i = 0; i<=yearsCount;i++){
for(var j = 0; j<12;j++){
//skipping the months before the month in old date and the after the month in the current day
if(i==0 && j<old.getMonth()) continue;
if(i==yearsCount && j>today.getMonth()) continue;
// creating a new date in the format : mm-dd-yyyy (with dd=01)
var newDate = new Date((j+1)+"-01-"+(old.getFullYear()+i))
//using to localestring to transfrom the date created into format : month year
var newmonth = newDate.toLocaleString('default', { month: 'long',year:'numeric' });
//pushing to the array
monthsArr.push(newmonth);
}
}
console.log(monthsArr);
还有改进的余地,但它完成了工作
此函数正在生成您提供的示例中的月份和年份列表。请注意,输入需要格式为 MONTH FULLYEAR
的字符串,而任何其他格式的字符串很可能会产生错误的结果。话虽如此,如果可能的话,我建议传递 Date
对象或时间戳
function generateMonthsAndYears(startingFromDateString) {
const cur = new Date(`15 ${startingFromDateString}`)
const untilDateString = new Date(new Date().getFullYear(), new Date().getMonth()+1, 15).toDateString();
const result = [];
for(; untilDateString !== cur.toDateString(); cur.setMonth(cur.getMonth()+1))
result.push(cur.toLocaleString('en-us', { month: 'long', year: 'numeric' }))
return result;
}
console.log(generateMonthsAndYears("December 2018"))
我正在尝试找出从过去到现在的年月组合。
所以可以说,过去的日期是
const date = "December 2019"
那我需要一个数组
const arrayOfMonths = [ "April 2020", "March 2020", "February 2020", "January 2020", "December 2019"]
您可以使用 JavaScript Date 对象和 while 循环。
只需从当前月份减去一个月,就可以得到您想要的日期。
像这样的东西应该有用。
https://jsfiddle.net/wsf61jpk/5/
var date="December 2018";
var result=[];
//set both start and end date to first date of the month
const end_date = new Date(date.replace(" ", " ,1 "));
const start_date = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
while(end_date<=start_date){
result.push(start_date.toLocaleString('default', { month: 'long' , year: 'numeric'}));
start_date.setMonth(start_date.getMonth() - 1);
}
console.log(result);
我是利用 javascript 日期函数
做到的var oldDate = "December 2019";
var old = new Date(Date.parse(oldDate));
var today = new Date();
var yearsCount= today.getFullYear() - old.getFullYear();
var monthsArr = [];
for(var i = 0; i<=yearsCount;i++){
for(var j = 0; j<12;j++){
//skipping the months before the month in old date and the after the month in the current day
if(i==0 && j<old.getMonth()) continue;
if(i==yearsCount && j>today.getMonth()) continue;
// creating a new date in the format : mm-dd-yyyy (with dd=01)
var newDate = new Date((j+1)+"-01-"+(old.getFullYear()+i))
//using to localestring to transfrom the date created into format : month year
var newmonth = newDate.toLocaleString('default', { month: 'long',year:'numeric' });
//pushing to the array
monthsArr.push(newmonth);
}
}
console.log(monthsArr);
还有改进的余地,但它完成了工作
此函数正在生成您提供的示例中的月份和年份列表。请注意,输入需要格式为 MONTH FULLYEAR
的字符串,而任何其他格式的字符串很可能会产生错误的结果。话虽如此,如果可能的话,我建议传递 Date
对象或时间戳
function generateMonthsAndYears(startingFromDateString) {
const cur = new Date(`15 ${startingFromDateString}`)
const untilDateString = new Date(new Date().getFullYear(), new Date().getMonth()+1, 15).toDateString();
const result = [];
for(; untilDateString !== cur.toDateString(); cur.setMonth(cur.getMonth()+1))
result.push(cur.toLocaleString('en-us', { month: 'long', year: 'numeric' }))
return result;
}
console.log(generateMonthsAndYears("December 2018"))