How to convert datetime into abbreviated date brazilian model

3

I have an object JSON that returns me an attribute datetime . However, I would like to display it in Brazilian short form. Example JUN 24, 2014 .

JSON attribute:

data {
    date: "2014-06-16 21:56:29"
}

I would like to do this by manipulating string, but this seems to be very "gambiarra". Does anyone know how best to do this? Remembering I use JavaScript.

    
asked by anonymous 24.06.2014 / 21:34

4 answers

5

You can do this:

function dataFormatada(d) {
    var nomeMeses = ['JAN', 'FEV', 'MAR', 'ABR', 'MAI', 'JUN', 'JUL', 'AGO', 'SET', 'OUT', 'NOV', 'DEC']
    var data = new Date(d);
    var dia = data.getDate();
    var mes = data.getMonth();
    mes = nomeMeses[mes];
    var ano = data.getFullYear();
    return [dia, mes, ano].join(' ');
}

var json = {
    data: {
        date: "2014-06-16 21:56:29"
    }
}

var data = dataFormatada(json.data.date);
alert(data); // dá 16 JUN 2014

Example online: link

The above code creates a function that takes a string like the one from your example and returns another string with the format you want.

The inner steps are more or less obvious. It may be worth mentioning that .getMonth() returns a number starting with 0 . That is, in the case of "January" the value would be 0 . Then it is possible to use directly as an index of the array that is at the beginning of the function where the abbreviations of the months of the year are stored.

If you want to change to small print, you can use .toLowerCase() , but as you asked the question in large print, I did so.

    
24.06.2014 / 21:45
4

You can use the methods of the Date object 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 have prepared this jsFiddle for you.

Explanation

day , month and year are variables where the numbers of the dates that you use will be stored - these numbers are taken by their% methods getDate() , getMonth() and getFullYear() that JavaScript itself comes.

In alert() , I'm using monthNames[date.getMonth()] because date.getMonth() returns the months in American format and it follows the indexing flow of the names of the months used in the monthNames vector, so they both coincide.     

24.06.2014 / 21:44
4

You can also let javascript determine which format is best

data = new Date("2014-06-16 21:56:29");

alert(data.toLocaleString());

//no navegador pt-BR vai dizer 16/6/2014 21:56:29
    
26.06.2014 / 06:41
3

You have a very good library, Momentjs , which has this purpose for converting dates to various types.

Code:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//code.jquery.com/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.6.0/moment-with-langs.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function(e) {
        $("#data").html(moment("2014-06-16 21:56:29").format("DD MMM YYYY"));
    });
</script>
</head>
<body>
    <div id="data"></div>
</body>
</html>

Example: JsFiddle p>     

24.06.2014 / 22:01