我的字符串为日期介于2018年3月至2018年6月。
现在我将在2018年3月和2018年6月使用2个变量。现在我希望将o / p设置为
2018年3月
2018年4月
2018年5月
2018年6月
代码:
var temp="Date between March 2018 and June 2018";
var fields=temp.split(' ');
var date2=fields[fields.length-2] +" "+fields[fields.length-1];
var date1=fields[fields.length-5] +" "+fields[fields.length-4];
alert(date1);//March 2018
alert(date2);//June 2018
答案 0 :(得分:1)
您可以为此使用moment.js:
const numberOfMonthsDiff = moment(date2).diff(moment(date1), 'months', true);
Documentation of the diff method。
默认情况下,moment#diff会将结果截断为零小数 位,返回一个整数。如果您想要一个浮点数, 将true作为第三个参数传递。在2.0.0之前,moment#diff返回a 四舍五入到最接近的整数,而不是四舍五入的数字。
修改
我了解您想要以下月份:
var temp="Date between March 2018 and June 2018";
var fields=temp.split(' ');
var date2=fields[fields.length-2] +" "+fields[fields.length-1];
var date1=fields[fields.length-5] +" "+fields[fields.length-4];
const numberOfMonthsDiff = moment(date2).diff(moment(date1), 'months', true);
const dateToModify = moment(date1);
const monthsBetween = Array.from(Array(numberOfMonthsDiff)).map(i => dateToModify.add(1,'months').format('MMMM YYYY'));
答案 1 :(得分:1)
如果您只想获取string
中两个月之间的月份名称:
array
的名称,其中所有months
的名称都已排序。string
中提取两个月。indexes
中获得两个months
中的array
。array
之间遍历indexes
并获得月份。这应该是这样的代码:
var temp = "Date between March 2017 and June 2018";
var dateStrings = temp.match(/(\w+\s\d{4})/gi);
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
let index1 = monthNames.indexOf(dateStrings[0].split(" ")[0]);
let index2 = monthNames.indexOf(dateStrings[1].split(" ")[0]);
let year1 = dateStrings[0].split(" ")[1];
let year2 = dateStrings[1].split(" ")[1];
var diff = +year2 - +year1;
var results = [];
if (diff == 0) {
for (var i = index1; i <= index2; i++) {
results.push(monthNames[i] + " " + year1);
}
} else {
for (var i = index1; i < monthNames.length; i++) {
results.push(monthNames[i] + " " + year1);
}
diff--;
var year = +year1 + 1;
for (var y = 0; y < diff; y++) {
for (var i = 0; i < monthNames.length; i++) {
results.push(monthNames[i] + " " + year);
}
year++;
}
for (var i = 0; i <= index2; i++) {
results.push(monthNames[i] + " " + year2);
}
}
演示:
var temp = "Date between March 2016 and June 2018";
var dateStrings = temp.match(/(\w+\s\d{4})/gi);
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
let index1 = monthNames.indexOf(dateStrings[0].split(" ")[0]);
let index2 = monthNames.indexOf(dateStrings[1].split(" ")[0]);
let year1 = dateStrings[0].split(" ")[1];
let year2 = dateStrings[1].split(" ")[1];
console.log(+year2 - +year1);
var diff = +year2 - +year1;
var results = [];
if (diff == 0) {
for (var i = index1; i <= index2; i++) {
results.push(monthNames[i] + " " + year1);
}
} else {
for (var i = index1; i < monthNames.length; i++) {
results.push(monthNames[i] + " " + year1);
}
diff--;
var year = +year1 + 1;
for (var y = 0; y < diff; y++) {
for (var i = 0; i < monthNames.length; i++) {
results.push(monthNames[i] + " " + year);
}
year++;
}
for (var i = 0; i <= index2; i++) {
results.push(monthNames[i] + " " + year2);
}
}
console.log(results);
答案 2 :(得分:1)
也许我们也需要考虑几年。 :-) https://jsfiddle.net/z07rwmnL/17/
var months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
var temp = "Date between March 2018 and June 2019";
var match = temp.match(/Date between (\S+) (\d+) and (\S+) (\d+)/);
if (match) {
var fromMonth = match[1];
var fromYear = parseInt(match[2], 10);
var toMonth = match[3];
var toYear = parseInt(match[4], 10);
if (toYear >= fromYear) {
var fromMonthIdx = months.indexOf(fromMonth);
var toMonthIdx = months.indexOf(toMonth);
while (toYear >= fromYear) {
var endIdx = (toYear > fromYear) ? 12 : toMonthIdx;
for (var i = fromMonthIdx + 1; i < endIdx; i++) {
console.log(months[i] + " " + fromYear);
}
fromMonthIdx = -1;
fromYear++;
}
}
}
答案 3 :(得分:1)
您可以简单地使用for循环在几个月之间获取距离,
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var temp="Date between March 2018 and June 2018";
var fields=temp.split(' ');
var date2=fields[fields.length-2] +" "+fields[fields.length-1];
var date1=fields[fields.length-5] +" "+fields[fields.length-4];
for(var i = new Date(date1).getMonth(); i <= new Date(date2).getMonth();i++) {
console.log(monthNames[i])
}
答案 4 :(得分:0)
我认为您可以使用正则表达式找到两个日期边界,然后遍历它以得到预期的输出。请注意,此解决方案适用于跨年(无异常处理,它假定您输入的字符串为恒定格式)。
var str = "Date between March 2017 and June 2018";
var startRE = /between (.*) and/g;
var endRE = /and (.*)/g;
var start = startRE.exec(str);
var end = endRE.exec(str);
var startDate = start[1]
var endDate = end[1]
var startMonth = startDate.split(' ')[0]
var startYear = startDate.split(' ')[1]
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var endMonth = endDate.split(' ')[0];
var endYear = endDate.split(' ')[1];
for(var y=startYear;y<=endYear; y++){
if(y==endYear){
if(startYear==endYear)
for(var m=monthNames.indexOf(startMonth);m<monthNames.indexOf(endMonth)-1;m++){
console.log("Output:"+monthNames[m] + " " + y);
}
else
for(var m=0;m<monthNames.indexOf(endMonth);m++){
console.log("Output:"+monthNames[m] + " " + y);
}
}
else{
if(y=startYear)
for(var m=monthNames.indexOf(startMonth);m<12;m++){
console.log("Output:"+monthNames[m] + " " + y);
}
else
{
for(var m=0;m<12;m++){
console.log("Output:"+monthNames[m] + " " + y);
}
}
}
}