Reputation: 3
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
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
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
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]);
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