Get current date in DD-Mon-YYY format in JavaScript/Jquery

258,242

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")
Share:
258,242

Related videos on Youtube

Tushar
Author by

Tushar

Software Developer at Ingenu (San Diego) pursuing Masters in Computer Science from San Diego State University.

Updated on July 09, 2022

Comments

  • Tushar
    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
      Dan Dascalescu over 5 years
      The 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
    Tushar over 9 years
    yes 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
    Ahmad over 9 years
    This is not an answer. Please post a valid applicable answer
  • Vince V.
    Vince V. over 9 years
    Normally this should work (tested in console): test = new Date(); test.format('d-M-Y')
  • wintvelt
    wintvelt about 8 years
    This 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
    roberto tomás over 7 years
    agreed. this would be great but format is not a function
  • NullPointer
    NullPointer about 7 years
    When executed in console TypeError: test.format is not a function test = new Date() test.format('d-M-Y');
  • Ahmad
    Ahmad over 6 years
    This answer does not meet the requirement of the OP. The required format is dd-Mon-yyyy not dd-MM-yyyy
  • Felix
    Felix over 6 years
    this is JS not Java
  • Roshana Pitigala
    Roshana Pitigala almost 6 years
    While 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
    Dan Dascalescu over 5 years
    You don't need to roll your own date functions. The Date object already has a method that does what the OP asked.
  • Dan Dascalescu
    Dan Dascalescu over 5 years
  • Dan Dascalescu
    Dan Dascalescu over 5 years
  • Klemen Tusar
    Klemen Tusar over 5 years
    Well yes. There is also the excellent date-fns library date-fns.org. I mean with today's npm packages it hardly makes sense to roll your own.
  • Sujatha Girijala
    Sujatha Girijala over 4 years
    my output format is Dec-11,-2019 which is not expected. please help me out
  • Mikhail Kh
    Mikhail Kh about 4 years
    thanks, answer above doesn`t work in my locale, but yours works fine!
  • Sushin Pv
    Sushin Pv about 4 years
    The perfect answer! Rest all answers will produce the result in a different order
  • Amy Doxy
    Amy Doxy about 4 years
    @SujathaGirijala did you find a solution?
  • RobG
    RobG about 4 years
    There 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
    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
    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
    Admin over 2 years
    Your 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
    Zsolt Meszaros over 2 years
    OP wanted DD-Mon-YYYY, your answer is DD/MM/YYYY.