I would like to ask for your help on this. We implemented a feature where if we selected a date from a calendar, it will display the date that is selected. While we can display the right date, it is not in the right format. I need to extract the dates from this sample statement: EndDateTime=2021-05-29T02%3A00%3A00%2B08%3A00T01%3A00%3A00.000Z,StartDateTime=2021-05-29T02%3A00%3A00%2B08%3A00T00%3A00%3A00.000Z

I need to extract the startdate and enddate using javascript. And it has to be in a DD-MMMM-YYYY format. Hopefully you can help me in here. thanks.


I would see if you can't get your output cleaned up a bit closer to the source. But since I am not familiar with your code, here's a method of extracting a date in the format dd-MMMM-YYYY from that string. Similar logic applies to the start date.

let str = 'EndDateTime=2021-05-29T02%3A00%3A00%2B08%3A00T01%3A00%3A00.000Z,StartDateTime=2021-05-29T02%3A00%3A00%2B08%3A00T00%3A00%3A00.000Z';
let parts = str.split(',');
// extract only the date you want and remove the URI encoding
let endDateTimeStamp = decodeURIComponent(parts[1].split('=')[1]);

// generate a date, drop the time portion as it will result in an invalid date
let endDate = new Date(endDateTimeStamp.split('T')[0]);

// JavaScript doesn't have much in the way of built-in methods for formatting dates. This is one option.
let year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(endDate);
let month = new Intl.DateTimeFormat('en', { month: 'long' }).format(endDate);
let day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(endDate);

let formattedEndDate = `${day}-${month}-${year}`;

Refer to the documentation on Intl.DateTimeFormat. If you have a locale (perhaps 'UK'?) that formats to dd-MMMM-YYYY then you may use that and eliminate the need to parse the date into individual components.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.