try123
try123

Reputation: 15

How can I convert the value I get with date range picker to two seperate date and time?

I need to get the start date, start time, end date and end time values ​​separately that I got with the daterangepicker.

startinnerDate = document.getElementById('reservationtime').value;

Incoming value: "07/14/2021 12:00 AM - 08/10/2021 11:00 PM"

how can i separate it?

Upvotes: 0

Views: 567

Answers (2)

Alex
Alex

Reputation: 131

Not the optimal one and not flexible, but very clear for understanding

const startinnerDate = "07/14/2021 12:00 AM - 08/10/2021 11:00 PM"
const [start, end]  = startinnerDate.split(' - ');
const [startDate, startTime, startTimePeriod] = start.split(' ');
const [endDate, endTime, endTimePeriod] = end.split(' ');
console.log('date >>> ', startDate, '| time >>> ', startTime + ' ' + startTimePeriod);
console.log('date >>> ', endDate, '| time >>> ', endTime + ' ' + endTimePeriod);

Upvotes: 0

Greedo
Greedo

Reputation: 3549

You can resolve with a couple of split:

const res = "07/14/2021 12:00 AM - 08/10/2021 11:00 PM";

function parseRes(res) {
  const [d1, d2] = res.split("-");
  const [departureDate, departureTime] = d1.trim().split(/\d\s\d/);
  const [arrivalDate, arrivalTime] = d2.trim().split(/\d\s\d/);
  return {departureDate, departureTime, arrivalDate, arrivalTime};
}

console.log(parseRes(res));

Upvotes: 1

Related Questions