I am trying to generate the current date in the Date Month, Year format. Currently I am using following code:
function currentDate()
{
var months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
];
var date = new Date();
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
return `${d} ${months[m]}, ${y}`;
}
Although, It's works perfectly. But, is there a shorter way to do this..?
Something like:
(new Date()).format('d M, Y')
Unfortunately, there isnt.
But you can use libraries like luxon:
DateTime.now().toFormat('d M, Y')
Or, if you want to stay native, you can use Intl.DateTimeFormat:
new Intl.DateTimeFormat('es-US', { dateStyle: 'full' }).format(d);
However, ECMAScript currently has a proposal to improve Date class by adding Temporal global object:
Temporal.now.date().toLocaleString('es-US', {
weekday: 'long',
});