Get current date in DD-Mon-YYY format in JavaScript/Jquery
There is no native format in javascript for DD-Mon-YYYY
.
You will have to put it all together manually.
The answer is inspired from : How to format a JavaScript date
// Attaching a new function toShortFormat() to any instance of Date() class
Date.prototype.toShortFormat = function() {
let monthNames =["Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep", "Oct","Nov","Dec"];
let day = this.getDate();
let monthIndex = this.getMonth();
let monthName = monthNames[monthIndex];
let year = this.getFullYear();
return `${day}-${monthName}-${year}`;
}
// Now any Date object can be declared
let anyDate = new Date(1528578000000);
// and it can represent itself in the custom format defined above.
console.log(anyDate.toShortFormat()); // 10-Jun-2018
let today = new Date();
console.log(today.toShortFormat()); // today's date
You can use toLocaleDateString and hunt for a format that's close to DD-mmm-YYYY (hint: 'en-GB'; you just need to replace the spaces with '-').
const date = new Date();
const formattedDate = date.toLocaleDateString('en-GB', {
day: 'numeric', month: 'short', year: 'numeric'
}).replace(/ /g, '-');
console.log(formattedDate);