How to convert a date that is in the format yyyy-mm-dd. For example: 2014-12-12 to Dec 12 2014?

Dani AI

Generated

A few quick notes building on the existing answers from , and : splitting the string is simple and the Date-based approach is common, but both can be brittle. Relying on the Date parser for ISO-like strings can produce different results across engines (time zones or older browsers), and manual month arrays are easy to mistype. A robust pattern is: validate and parse the yyyy-mm-dd parts, construct a Date from numeric parts (avoids Date.parse quirks), then format the month with Intl so you get the correct short name for the locale.

function ymdToMmmDdY(ymd) {
  var m = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(ymd);
  if (!m) return null; // invalid format
  var y = Number(m[1]), mo = Number(m[2]) - 1, d = Number(m[3]);
  var date = new Date(y, mo, d);
  if (date.getFullYear() !== y || date.getMonth() !== mo || date.getDate() !== d) return null; // invalid date like 2014-02-30
  var month = new Intl.DateTimeFormat('en-US', { month: 'short' }).format(date); // "Dec"
  return month + ' ' + date.getDate() + ' ' + date.getFullYear(); // "Dec 12 2014"
}

Troubleshooting tips: validate input with the regex above if your source can vary; check the constructed Date to detect invalid calendar dates; use Intl.DateTimeFormat for correct, local-aware short month names (fall back to a small array or a polyfill only when Intl is unavailable). This approach preserves the exact output format "Dec 12 2014" while avoiding parser and timezone surprises that can show up years later or in different browsers.

var oldDate = '2013-4-18';
var newDate = null;
var arrayMonth = ['Jan', 'Feb', 'Mar', 'Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Des'];
var help = oldDate.split('-');
newDate = arrayMonth[help[1] - 1] + " " + help[2] + " " + help[0];
console.log(newDate);

Try this following script to convert date to yyyy-mm-dd to mmm dd yyyy..

function formatDate(d)
{
    var date = new Date(d);

    var month = new Array();
    month[0] = "Jan";
    month[1] = "Feb";
    month[2] = "Mar";
    month[3] = "Apr";
    month[4] = "May";
    month[5] = "Jun";
    month[6] = "Jul";
    month[7] = "Aug";
    month[8] = "Sept";
    month[9] = "Oct";
    month[10] = "Nov";
    month[11] = "Dec";

    day = date.getDate();
    return month[date.getMonth()] +" "+day + " " + date.getFullYear();
}
date_response = formatDate(' 2014-12-12');
console.log(date_response);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.