如何将使用日期范围选择器获得的值转换为两个单独的日期和时间?
How can I convert the value I get with date range picker to two seperate date and time?
我需要分别获取我用daterangepicker获取的开始日期、开始时间、结束日期和结束时间值。
startinnerDate = document.getElementById('reservationtime').value;
传入值:“07/14/2021 12:00 AM - 08/10/2021 11:00 PM”
如何分开?
你可以通过几个拆分来解决:
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));
不是最优的,不灵活,但是很清楚理解
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);
我需要分别获取我用daterangepicker获取的开始日期、开始时间、结束日期和结束时间值。
startinnerDate = document.getElementById('reservationtime').value;
传入值:“07/14/2021 12:00 AM - 08/10/2021 11:00 PM”
如何分开?
你可以通过几个拆分来解决:
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));
不是最优的,不灵活,但是很清楚理解
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);