-1

I am working with two given dates and want to show them in my calendar. I'm using react-native-calendars. Here i need to pass an array to show in my calendar like this:

    const calendarData = [
      { date: '2021-04-01', status: 'available' },
      { date: '2021-04-02', status: 'available' },
      { date: '2021-04-03', status: 'available' },
      { date: '2021-04-04', status: 'booked' },
      { date: '2021-04-05', status: 'booked' },
      { date: '2021-04-06', status: 'available' },
      { date: '2021-04-07', status: 'available' },
      { date: '2021-04-08', status: 'booked' },
      { date: '2021-04-09', status: 'available' },
      { date: '2021-04-27', status: 'available', selected: true },
    ];

But i have got my start date and finish date from my API.

var fromDate = '2021-04-01';
var toDate = '2021-04-15';
 

From these two dates, how can i generate an array like calendarData.

  • Does this answer your question? https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates – Slava Knyazev Apr 04 '21 at 06:14
  • In the question, they had add dates from the current date or so. But i get my dates from an API. it's not the current data. So, how can i do that. @SlavaKnyazev – Tanmoy Sarker Apr 04 '21 at 06:23
  • Are you looking to generate an array with all the dates between fromDate and toDate? If so, the link has everything you need. I'm not sure where you get the `status`. – Slava Knyazev Apr 04 '21 at 06:28
  • I'm using getDaysArray(new Date("2021-04-01"),new Date("2021-05-15")). but it shows getDaysArray is not defined in React Native. @SlavaKnyazev – Tanmoy Sarker Apr 04 '21 at 07:30

1 Answers1

0

You can use the date-fns library's eachDayOfInterval function to get an array of dates between your start and finish date.

You could then use Array.map to convert the date array to an array of objects with the "date" and "status" props

Simon Tran
  • 1,461
  • 1
  • 11
  • 28
  • I'm using getDaysArray(new Date("2021-04-01"),new Date("2021-05-15")). but it shows getDaysArray is not defined in React Native. – Tanmoy Sarker Apr 04 '21 at 07:30
  • You got "getDaysArray" from [this answer](https://stackoverflow.com/a/50398144/6592293)? You need to implement it. If you want to use this answer, you need to copy the code of the function. – Simon Tran Apr 04 '21 at 18:41