There are two ways to format a date a string in English US format.
- Using a reusable formatter instance
Intl.DateTimeFormat
- Calling
toLocaleDateString
const dateFormatter = new Intl.DateTimeFormat('en-US');
// Reusable formatter
console.log(dateFormatter.format(new Date()));
// Inline locale string
console.log(new Date().toLocaleDateString('en-US'));
If you want padding, you need to specify 2-digit
as an option for month and day.
Note: Don't forget the year!
const dateFormatter = new Intl.DateTimeFormat('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
});
// Reusable formatter
console.log(dateFormatter.format(new Date()));
// Inline locale string
console.log(new Date().toLocaleDateString('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
}));
Now, to put it all together:
const dateFormatter = new Intl.DateTimeFormat('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
});
const parseDate = (dateStr) => {
const tokens = dateStr.split(/\s/g); // <MONTH_INDEX|MONTH_NAME> <YEAR>
if (tokens.length !== 2) throw new Error(`Illegal date format: ${dateStr}`)
return !isNaN(dateStr[1])
? new Date(+tokens[1], +tokens[0] - 1, 1) // Numeric month
: new Date(Date.parse(`${tokens[0]} 1, ${tokens[1]}`)); // Month name
};
['6 2023', 'June 2023', 'foobar'].forEach((dateStr) => {
try {
console.log(dateFormatter.format(parseDate(dateStr)));
} catch (e) {
console.log(e.message);
}
});
Output
06/01/2023
06/01/2023
Here is a better version that is more efficient at parsing the month name. All the months for the en-US
are generated ahead of time.
const getLocalMonthNames = (locale = 'default', format = 'long') =>
Array.from({ length: 12 }, (_, monthIndex) =>
new Date(0, monthIndex, 1).toLocaleString(locale, { month: format }));
const monthNames = getLocalMonthNames('en-US');
const dateFormatter = new Intl.DateTimeFormat('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
});
const parseDate = (dateStr) => {
const tokens = dateStr.split(/\s/g); // <MONTH_INDEX|MONTH_NAME> <YEAR>
if (tokens.length !== 2) throw new Error(`Illegal date format: ${dateStr}`);
const monthIndex = !isNaN(dateStr[1])
? +tokens[0] - 1 // Numeric month
: monthNames.indexOf(tokens[0]); // Month name
return new Date(+tokens[1], monthIndex, 1);
};
// Convenience (parse and format)
const convertDate = (dateStr) => dateFormatter.format(parseDate(dateStr));
['6 2023', 'June 2023', 'foobar'].forEach((dateStr) => {
try {
console.log(convertDate(dateStr));
} catch (e) {
console.log(e.message);
}
});
Vue example
Since you are using Vue, you can just define the function(s) outside of the Vue application and call the convertDate
function when computing the model.
const getLocalMonthNames = (locale = 'default', format = 'long') =>
Array.from({ length: 12 }, (_, monthIndex) =>
new Date(0, monthIndex, 1).toLocaleString(locale, { month: format }));
const monthNames = getLocalMonthNames('en-US');
const dateFormatter = new Intl.DateTimeFormat('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric'
});
const parseDate = (dateStr) => {
const tokens = dateStr.split(/\s/g); // <MONTH_INDEX|MONTH_NAME> <YEAR>
if (tokens.length !== 2) throw new Error(`Illegal date format: ${dateStr}`);
const monthIndex = !isNaN(dateStr[1])
? +tokens[0] - 1 // Numeric month
: monthNames.indexOf(tokens[0]); // Month name
return new Date(+tokens[1], monthIndex, 1);
};
const convertDate = (dateStr) => dateFormatter.format(parseDate(dateStr));
new Vue({
el: '#app',
data() {
return {
input: 'June 2023'
}
},
computed: {
output: function() {
return convertDate(this.input);
}
}
});
html, body, #app {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#app {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 0.25rem;
}
#app input {
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>Date String Converter</h2>
<input v-model="input">
<p>{{ output }}</p>
</div>