I have some events that pulls its date like the following:
20th June 10am - 5th September 10pm
28th September noon - 4pm
I've not control of what it returns, so I need to remove it via javascript
I need to remove the time and replace the months with 3 character version, like the below:
20th Jun - 5th Sep
I've started doing it but I don't know how to remove the time, because it could be either formatted with AM/PM or MIDNIGHT/NOON
This is what I've done so far:
var dateExample = '2nd August 9am - 27th September midnight';
function trimDate(date){
const dateArray = date.split(' ');
var months = [['January', 'Jan'], ['February', 'Feb'], ['March', 'Mar'], ['April', 'Apr'], ['May', 'May'], ['June', 'Jun'], ['July', 'Jul'], ['August', 'Aug'], ['September', 'Sep'], ['October', 'Oct'], ['November', 'Nov'], ['December', 'Dec']];
for (let x = 0; x < months.length; x++){
let i = dateArray.findIndex(d => d === months[x][0]);
if (i > -1) dateArray.splice(i, 1, months[x][1]);
}
console.log(dateArray.join(' '));
}
trimDate(dateExample);