Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase day number with every map loop (Moment.js)?

I'm making a simple week calendar in React where I need to display columns with hours. A calendar will be generated onClick and based on the actual week number will show the whole week mon-sun. I'm using Moment.js to manage date.

What I'm trying to do is to increase the day number by 1 with every map loop. Right now I know how to display the right day number for the actual day. But how can I increase it by 1 with every loop? I tried this solution How increment id tag with map function in React . It works on id, but it didn't work with this example.

I display the day number using this code:

{moment(date).format(`DD/MM/YYYY`)}

I'd be glad for any tips or advice. Here is a little glimpse at my code:

const CalendarAdmin = ({openModal}) => {

    const { active, setActive, sorted } = useContext(CalendarContext)

    function addCalendar() {
        setShow(true);
    }

    const [show, setShow] = useState(false);

    const date = new Date();    
    const currentDate = moment(date).format('DD/MM/YYYY');
    const currentWeek = moment(date).format('w');
    const currentDay = moment(date).format('D');

    const renderWeek = () => {
        return (
        <div className="calendar-content">
                <h1>Calendar 1</h1>
                <h1>Okres czasu: 14.11 - 20.11 ({currentWeek})</h1>
                <div className="calendar-days"> 
                    
                    {dayNames.map((item, dayIndex) => 
                        <div className="day-column" >
                            <h2 key={dayIndex}>{item.longName}</h2>
                            <h2>{item.testDate}</h2>
                              
                            {moment(date).format(`DD/MM/YYYY`)}
                                                       
                            {hours.map((hour, hourIndex) => {...
                       
like image 933
MarcinB Avatar asked Nov 26 '25 03:11

MarcinB


1 Answers

You can use this add function in moment js as

arrayToMap.map((val,i) => {
  const date = new Date(); 
  return moment(date).add(i, 'd');
})
like image 72
anoop francis Avatar answered Nov 27 '25 17:11

anoop francis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!