Home > Back-end >  How to convert "['2022-03-24', '2022-03-25']" to javascript array
How to convert "['2022-03-24', '2022-03-25']" to javascript array

Time:03-17

I want to convert "['2022-03-24', '2022-03-25']" to javascript array data is from element.dataset

did const dateRange = JSON.stringify("['2022-03-24', '2022-03-25']") JSON.parse(dateRange) returns ['2022-03-24', '2022-03-25'] which is a string and not a javaScript array.

I want to populate date range with flatpickr with object from my database so I'm passing the date html element via data attribute <input type="date" data-date="{{date_range}}" />

CodePudding user response:

Try that, it would leave you with array of date strings, try to parse them to dates now (if you need).

someString.replace(/[',\]\[]/g, '').split(' ')
  • Related