Rizky
Rizky

Reputation: 3

Javascript convert date format from "yyyy-mm-dd" to "December 11th, 2013"?

How I make a sintax to convert date format from default format (yyyy-mm-dd) to english format like December 11th, 2013 using Javascript function?

Thank you

Upvotes: 0

Views: 176

Answers (3)

wayne
wayne

Reputation: 3410

You could use moment.js

Moment.js 2.7.0

Moment was designed to work both in the browser and in Node.JS. Parse, validate, manipulate, and display dates in javascript.

and it is also available on cdnjs.com.

Upvotes: 3

gloover
gloover

Reputation: 11

You can use a function like this:

function formatDate(date) {
  months = ['January', 'February', 'March', 'April',
            'May', 'June', 'July', 'August',
            'September', 'October', 'November', 'December'];
  dateSplit = date.split('-');
  year = dateSplit[0];
  month = months[parseInt(dateSplit[1]) - 1];
  day = parseInt(dateSplit[2]);
  switch(day) {
    case 1:
    case 21:
    case 31:
      day += 'st';
      break;
    case 2:
    case 22:
      day += 'nd';
      break;
    case 3:
    case 23:
      day += 'rd';
      break;
    default:
      day += 'th';
  }
  return month + ' ' + day + ', ' + year;
}

Upvotes: 1

Akshay Mohite
Akshay Mohite

Reputation: 109

var str_date = "1983-24-12",
    arr_date = str_date.split('-'),
    months   = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

var new_date=months[arr_date[2]] + ' ' + arr_date[1] + ', ' + arr_date[0]);

http://jsfiddle.net/TN7NE/

Edit

You can add an ordinal using the following:

function addOrdinal(n) {
    var ord = [,'st','nd','rd'];
    var a = n%100;
    return n + (ord[a>20? a%10 :a] || 'th');
}

e.g.

addOrdinal(1); // 1st

Upvotes: 1

Related Questions