gpt4 book ai didi

javascript - momentjs 将 moment 对象添加到数组中

转载 作者:行者123 更新时间:2023-12-02 17:26:26 25 4
gpt4 key购买 nike

js:

service.search = function (goDate, returnDate) {

var outwardInterval = {};
outwardInterval.start = moment(goDate, 'YYYY-MM-DD').subtract(3, 'day');
outwardInterval.end = moment(goDate, 'YYYY-MM-DD').add(3, 'day');

matrice.outwardDates = buildDateArray(outwardInterval);

}
var buildDateArray = function (interval) {
var array = [];
var currentDate = interval.start;
do {
array.push(currentDate);
currentDate.add(1, 'day');
} while (!currentDate.isAfter(interval.end));

return array;
};

我的输出:

enter image description here

为什么在我的数组中我有相同的值..?

更新:

JSFIDDLE

最佳答案

扩展我的评论:

function GetDates(startDate, daysToAdd) {
var aryDates = [];
for(var i = 0; i <= daysToAdd; i++) {
var currentDate = new Date();
currentDate.setDate(startDate.getDate() + i);
aryDates.push(DayAsString(currentDate.getDay()) + ", " + currentDate.getDate() + " " + MonthAsString(currentDate.getMonth()) + " " + currentDate.getFullYear());
}

return aryDates;
}

function MonthAsString(monthIndex) {
var d=new Date();
var month=new Array();
month[0]="Jan";
month[1]="Feb";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="Aug";
month[8]="Sep";
month[9]="Oct";
month[10]="Nov";
month[11]="Dec";

return month[monthIndex];
}

function DayAsString(dayIndex) {
var weekdays = new Array(7);
weekdays[0] = "Sun";
weekdays[1] = "Mon";
weekdays[2] = "Tue";
weekdays[3] = "Wed";
weekdays[4] = "Thu";
weekdays[5] = "Fri";
weekdays[6] = "Sat";

return weekdays[dayIndex];
}

var startDate = new Date();
var aryDates = GetDates(startDate, 7);
console.log(aryDates);

fiddle :http://jsfiddle.net/u93g87qc/2/

关于javascript - momentjs 将 moment 对象添加到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27687088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com