【问题标题】:How can I iterate throught array in loop and calculate dates between these with RN and Moment.js如何在循环中遍历数组并使用 RN 和 Moment.js 计算这些日期之间的日期
【发布时间】:2021-08-24 15:59:39
【问题描述】:

我正在使用 React Native Calendar 做一些事情。我需要获取两个日期之间的所有日期并在 React Native Calendar 中标记这些日期。 我有现在在数组中的开始日期和结束日期,我也可以在这些日期之间有日期,但前提是我使用我想要的数组中的值指定。这个想法是我希望该函数获取所有第一个/第二个/第三个等值并自动计算这些值之间的日期,最后在我的日历中标记这些值。实际上,我无法在此日历上记录所有日期。

我该怎么做?提前谢谢你,这是我的代码:

    try {
        let res = await axios.get(`https://myseenapp.herokuapp.com/constructionSite/${_id}`);
        let data = res.data;
        setConstruction(data);

        setStartDate(
            data
                .map(e => e.startDate)
                .forEach((day) => {
                    let convert = day.split('-').reverse().join('-')
                    if (arrayStart.includes(convert) === false) {
                        arrayStart.push(convert)
                    }
                }));
        setEndDate(
            data
                .map(e => e.endDate)
                .forEach((day) => {
                    let convert = day.split('-').reverse().join('-')
                    if (arrayEnd.includes(convert) === false) {
                        arrayEnd.push(convert)
                    }
        }));
    }
    catch (err) {
        console.log(err)
    }
};

let end = moment(arrayEnd[0]);
let start = moment(arrayStart[0]);


const getDaysBetweenDates = (start, end) => {
    let now = start;
    let dates = [];
    while (now <= end){
        dates.push(now.format("YYYY-MM-DD"));
        now.add(1, 'days');
        dates.forEach((day) => {
            newDaysObject[day] = {
                textColor: "white",
                color: 'gold',
                selected: true,
            }
        })
    }
    return dates;
}
let dateList = getDaysBetweenDates(start, end);
console.log(dateList)

【问题讨论】:

  • 但是你的问题是计算所有日期? o 在您的日历中插入这个日期?
  • 您好,我的问题是我无法通过获取 startDate 数组和 EndDate 数组的第一个值来计算所有日期。仅当我指定要使用 arrayEnd[0] 和 arrayStart[0] 采用哪个值时它才有效,但如果我创建一个 forEach 则不会。

标签: javascript arrays react-native foreach momentjs


【解决方案1】:
    const getDaysBetweenDates = (start, end) => {
        let now = start;
        let dates = [];
        while (now <= end){
            dates.push(now.format("YYYY-MM-DD"));
           now = momet(now).add(1, 'days'); // you are updating the same object again and again, so here I am creating new object
      
        }
     // moving this out so it runs only when all the dates are filled in array
// this should have been done outside this method for cleaner code

    dates.forEach((day) => { 
// not sure where this object came from.
                newDaysObject[day] = {
                    textColor: "white",
                    color: 'gold',
                    selected: true,
                }
    });
        return dates;
    }

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多