Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 859
Tushar Avatar asked Dec 15 '14 08:12

Tushar


People also ask

How to get Date in dd mon yyyy format in JavaScript?

javascript date today dd mm yyyyvar mm = String(today. getMonth() + 1). padStart(2, '0'); //January is 0!

How to set current Date using jQuery?

To set current date in control to which jQuery UI datepicker bind, use setDate() method. Pass date object which needs to be set as an argument to setDate() method. If you want to set it to current date then you can pass 'today' as argument.


1 Answers

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
like image 174
Ahmad Avatar answered Sep 28 '22 13:09

Ahmad