Im working on an app that uses a few helper functions that I've created to format data coming in. Below is the code that I've made. It all works, and does what it's supposed to, but I'm hoping for some help with making the code a bit more efficient.. I feel that it could be cleaned up a bit.
// Convert to Title Case
export function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
// Convert date from ISO-8601 to toDateString without time
export function formatDate(dateData) {
const date = new Date(dateData);
const formattedDate = date.toDateString();
return formattedDate;
}
// Convert date from ISO-8601 to toDateString with time
export function formatDateWithTime(dateData) {
const date = new Date(dateData);
const formattedDate = date.toLocaleString();
return formattedDate;
}