You can use the object methods Date
Javascript to do this. See:
var data = {
date: "2014-06-16 21:56:29"
};
var date = new Date(data.date),
day = date.getDate(),
month = date.getMonth(),
year = date.getFullYear(),
monthNames = ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'];
alert(day + ' de ' + monthNames[date.getMonth()] + ' de ' + year);
To test, I prepared this jsFiddle for you.
Explanation
day
, month
and year
are variables where the numbers of the dates you use will be stored - these numbers are picked by the respective methods getDate()
, getMonth()
and getFullYear()
that Javascript itself provides.
In the alert()
, I’m using monthNames[date.getMonth()]
because date.getMonth()
returns the months in the American format and it follows the indexing flow of the names of the months used in the vector monthNames
, therefore, both coincide.
It works perfectly, but when testing in browsers other than Google Chrome it shows a strange behavior. Its return is "Nan Nan". Can you help me with that?
– Phellipe Lins