H Varma H Varma - 1 year ago 103
Javascript Question

How to convert yyyy-mm-dd to dd-Mon-yyyy using javascript

I have a date format as 2016-01-09.Now I want to convert this date format in to 09-Jan-2016 I was able to convert in to 09-01-2016.But how can i get the month name added there.can someone help..
Here is the code which iam trying

function formatDate(input){
var datePart = input.match(/\d+/g),
year = datePart[0].substring(0,4), // get only two digits
month = datePart[1], day = datePart[2];

return day+'-'+month+'-'+year;

formatDate ('2010-01-18'); // "18-01-2010"

Answer Source

You can do this in multiple ways as follows:

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function dateFormat(d){
  var t = new Date(d);
  return t.getDate()+'-'+monthNames[t.getMonth()]+'-'+t.getFullYear();


You can get month in this way:

var objDate = new Date(date).toLocaleString("en-us", { month: "long" });//January
var objDate = new Date(date).toLocaleString("en-us", { month: "short" }); //Jan


You can use moment.js

var shortMonthName = moment(date).format('MMM') // Jan
var fullMonthName = moment(date).format('MMMM') // January


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download