Get current date in DD-Mon-YYY format in JavaScript/Jquery
I need to get the date format as 'DD-Mon-YYYY' in javascript. I had asked a question, and it got marked duplicate to jQuery date formatting
But, the answers provided in the question are to get the current date in "DD-MM-YYYY" format and not "DD-MON-YYYY". Secondly, I am not using datepicker plugin.
Can you please help me as if how to get the current date in "DD-Mon-YYYY" format.
Solution 1:
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
Solution 2:
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);
Solution 3:
Use the Moment.js library http://momentjs.com/ It will save you a LOT of trouble.
moment().format('DD-MMM-YYYY');
Solution 4:
Can be done with toLocaleDateString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
<script>
const date = new Date();
const formattedDate = date.toLocaleDateString('en-GB', {
day: '2-digit', month: 'short', year: 'numeric'
}).replace(/ /g, '-');
document.write(formattedDate);
</script>
Solution 5:
I've made a custom date string format function, you can use that.
var getDateString = function(date, format) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
getPaddedComp = function(comp) {
return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
},
formattedDate = format,
o = {
"y+": date.getFullYear(), // year
"M+": months[date.getMonth()], //month
"d+": getPaddedComp(date.getDate()), //day
"h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
"H+": getPaddedComp(date.getHours()), //hour
"m+": getPaddedComp(date.getMinutes()), //minute
"s+": getPaddedComp(date.getSeconds()), //second
"S+": getPaddedComp(date.getMilliseconds()), //millisecond,
"b+": (date.getHours() >= 12) ? 'PM' : 'AM'
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
formattedDate = formattedDate.replace(RegExp.$1, o[k]);
}
}
return formattedDate;
};
And now suppose you've :-
var date = "2014-07-12 10:54:11";
So to format this date you write:-
var formattedDate = getDateString(new Date(date), "d-M-y")