Issue
I need to format this string so I cand send it to an angular app using fullcalendar for its calendar, so i can concatenate the start hour of the appointment to the date.
Thank you very much!, this is my first question here...
I've tried to split the string, parse it to number, and then add conditional operators.
Solution
This converts a time interval e.g. '8-9 AM', to an ISO time interval as recommended by Liam
let timeString = '8-9 AM'
function to24Hour(timeStr) {
let spaceIndex = timeStr.indexOf(' ');
let beforePM = timeStr.split(' ')[1] === 'AM' ? true : false //get if before PM
timeStr = timeStr.split(' ')[0]; //split at space and select interval string
if (!timeStr.includes('-')) return 'invalid interval'; //if no interval
let startTime, endTime, interval, startEndArr = timeStr.split('-');
startTime = startEndArr[0];
endTime = startEndArr[1];
if (!beforePM) {
startTime = Number(startTime) + 12;
endTime = Number(endTime) + 12; //PM accomodation
};
if (startTime.length === 1) startTime = `0${startTime}`; //append 0 to front
if (endTime.length === 1) endTime = `0${endTime}`;
interval = endTime - startTime; //get interval
if (beforePM) {
return `P${interval}H/${startTime}:00Z` //return AM formatted ISO interval
} else {
return `P${interval}H/${startTime}:00Z` //return PM formatted ISO interval
};
}
console.log(to24Hour(timeString));
Answered By - meropis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.