If you want to format dates using built-in methods of javascript then you can use Intl.DateTimeFormat() of javascript.
Here’s how you can format date to ’mm dd, yy’ format using Intl.DateTimeFormat():
Code:
const formatDate = (date) => {
const options = {
year: '2-digit',
month: 'short',
day: 'numeric',
};
const formatter = new Intl.DateTimeFormat('en-US', options);
const parts = formatter.formatToParts(date);
// Extract the formatted parts of date to rearrange them
const month = parts.find(part => part.type === 'month').value;
const day = parts.find(part => part.type === 'day').value;
const year = parts.find(part => part.type === 'year').value;
return `${month} ${day}, ${year}`;
}
const myDate = new Date(); // You can replace this with your date
const formattedDate = formatDate(myDate);
console.log(formattedDate);
Below are variety of options you can use to format date according to your requirements:
For Year:
‘numeric’: 2023
‘2-digit’: 23
For Month:
‘numeric’: 1, 2, 3, …
‘2-digit’: 01, 02, 03, …
‘narrow’: J, F, M, …
‘short’: Jan, Feb, Mar, …
‘long’: January, February, March, …
For Day:
‘numeric’: 1, 2, 3, …
‘2-digit’: 01, 02, 03, …
For Weekday:
‘narrow’: S, M, T, …
‘short’: Sun, Mon, Tue, …
‘long’: Sunday, Monday, Tuesday, …
For Hour:
‘numeric’: 1, 2, 3, …
‘2-digit’: 01, 02, 03, …
For Minute:
‘numeric’: 0, 1, 2, …
‘2-digit’: 00, 01, 02, …
For Second:
‘numeric’: 0, 1, 2, …
‘2-digit’: 00, 01, 02, …
You can also use the popular moment.js library to easily format your dates.
Work with our skilled Javascript developers to accelerate your project and boost its performance.
Hire JavaScript Developers