Get current date in DD-Mon-YYY format in JavaScript/Jquery
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
<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")
Related videos on Youtube
Tushar
Software Developer at Ingenu (San Diego) pursuing Masters in Computer Science from San Diego State University.
Updated on July 09, 2022Comments
-
Tushar almost 2 years
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.
-
Dan Dascalescu over 5 yearsThe currently accepted answer is overly complicated. I've edited another answer that I think answers your question better. Can you please take a look?
-
-
Tushar over 9 yearsyes I understand the date format, but how can I convert current date to dd-mon-yyyy, Eg I want to get current date in 15-Dec-2014, format.
-
Ahmad over 9 yearsThis is not an answer. Please post a valid applicable answer
-
Vince V. over 9 yearsNormally this should work (tested in console): test = new Date(); test.format('d-M-Y')
-
wintvelt about 8 yearsThis is a very low quality answer: please provide explanation of what your code does. Also, this code does not take parameter, but does expect a global variable
d
. Finally, it does not answer OPs question: YYYY/MM/DD is not the same as DD-Mon-YYY that was asked for. -
roberto tomás over 7 yearsagreed. this would be great but format is not a function
-
NullPointer about 7 yearsWhen executed in console
TypeError: test.format is not a function test = new Date() test.format('d-M-Y');
-
Ahmad over 6 yearsThis answer does not meet the requirement of the OP. The required format is dd-Mon-yyyy not dd-MM-yyyy
-
Felix over 6 yearsthis is JS not Java
-
Roshana Pitigala almost 6 yearsWhile this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.
-
Dan Dascalescu over 5 yearsYou don't need to roll your own date functions. The Date object already has a method that does what the OP asked.
-
Dan Dascalescu over 5 years...or you could have improved the existing answer that uses toLocaleDateString.
-
Dan Dascalescu over 5 yearsIt will also bloat your code a lot and is unnecessary when JavaScript core can solve the problem without libraries.
-
Klemen Tusar over 5 yearsWell yes. There is also the excellent
date-fns
library date-fns.org. I mean with today'snpm
packages it hardly makes sense to roll your own. -
Sujatha Girijala over 4 yearsmy output format is Dec-11,-2019 which is not expected. please help me out
-
Mikhail Kh about 4 yearsthanks, answer above doesn`t work in my locale, but yours works fine!
-
Sushin Pv about 4 yearsThe perfect answer! Rest all answers will produce the result in a different order
-
Amy Doxy about 4 years@SujathaGirijala did you find a solution?
-
RobG about 4 yearsThere are a large number of date libraries, should there be an answer for every one of them? Many are much smaller than moment.js.
-
Klemen Tusar about 4 years@RobG Indeed there are now, but back in 2014, when I gave this answer, there really wasn't much to play with.
-
Calsal over 3 years@SujathaGirijala that's probably because of the locale you're using. 'en-GB' is used in this example and that should give you DD Mon YYYY. If you use locale 'en-US' you get Mon DD, YYYY.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
Zsolt Meszaros over 2 yearsOP wanted
DD-Mon-YYYY
, your answer isDD/MM/YYYY
.