0

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);
0

2 Answers 2

1

It looks like it should be possible with a single regex:

const dateExamples = [
  '2nd August 9am - 27th September midnight',
  '20th June 10am - 5th September 10pm',
  '2nd August noon - 27th September midnight'
]
const regex = /[0-9]{1,2}[ndrths]{2}[\s]{1}[\w]{3}/g
dateExamples.forEach(dateExample => console.log(dateExample.match(regex).join(' - ')))

Sign up to request clarification or add additional context in comments.

2 Comments

Much better than my weak attempt
ha, yours was helpful actually!
1

Replacing the dates can be done much simpler using regex:

var dateExample = '2nd August 9am - 27th September midnight';
var re = /January|February|March|April|May|June|July|August|September|October|November|December/gi;
var replaced = dateExample.replace(re, m => m.substring(0,3));
console.log(replaced)

You could use a similar trick for the times, the regex might be a bit more complex:

var dateExample = '2nd August 9am- 27th September midnight';
var datesRegex = /January|February|March|April|May|June|July|August|September|October|November|December/gi;


var timesRegex = /\w*([0-9]{1,2}(am|pm|AM|PM))|(noon|midnight)\w*/gi
var replaced = dateExample.replace(datesRegex, m => m.substring(0,3))
                           .replace(timesRegex,'');
console.log(replaced)

Putting it all together

function clean(input){

  var datesRegex = /January|February|March|April|May|June|July|August|September|October|November|December/gi;
  var timesRegex = /\w*([0-9]{1,2}(am|pm|AM|PM)\w*)|(noon|midnight)/gi
  return input.replace(datesRegex, m => m.substring(0,3))
                             .replace(timesRegex,'');
}

// test cases
console.log(clean("20th June 10am - 5th September 10pm"));
console.log(clean("2nd August 9am - 27th September midnight"));
console.log(clean("2nd August noon - 27th September midnight"));

1 Comment

this is a great answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.