Reputation: 579
Does anyone know a jquery date plugin, or an alternative jquery/javascript method to transform a default formatted date or a dd/mm/yyyy to a 'human readable' date for example
5/04/2013
5th April 2013
? Iunderstand this could be done manually with making our own array etc...
Upvotes: 1
Views: 4346
Reputation: 14429
No need to use a library...keep it simple and unbloated...
http://jsbin.com/oyekaj/1/edit
var date = '5/04/2013';
var months = ['January','February','March','April','May','June','July','Auguest','September','October','November','December'];
function daySuffix(d) {
d = String(d);
return d.substr(-(Math.min(d.length, 2))) > 3 && d.substr(-(Math.min(d.length, 2))) > 21 ? "th" : ["th", "st", "nd", "rd", "th"][Math.min(Number(d)%10, 4)];
}
var dataArray = date.split("/");
var newDate = new Date(dataArray[2], dataArray[1]-1, dataArray[0]);
var dateString = newDate.getDate() + daySuffix(newDate.getDate()) + " " + months[newDate.getMonth()] + " " + (newDate.getYear()+1900);
console.log(dateString);
Upvotes: 1
Reputation: 679
The most awesome plugin I have used for date parsing which has multiple functionalities is:
Moment supports multiple date formats, can display time as a factor of duration elapsed, can read multiple formats compared to the native JavaScript library.
Upvotes: 1
Reputation: 2550
This is pretty awesome and stable: http://www.datejs.com/
Upvotes: 0