Which format is most appropriate for the LocalDate field in JSON?

0

I have seen date fields returned in the following ways:

"2012-04-23"   //Padrão ISO 8601
"23/04/2012"   //Pronto para renderização no front-end

Considering the use and flexibility of the front-end, such as performing a search on a grid with AngularJS, ordering, etc. Which format is most appropriate or is there the right format?

    
asked by anonymous 09.02.2017 / 18:46

1 answer

1

I think it's ideal that the backend send timestamp to the front and it suits you as needed.

Ordering is best to use with timestamp because it is just a number, in Java will be long or wrapper Long .

Example:

var datasOrdenadas = datasEmTimestamp.sort(function(data1, data2) {
  return data1 > data2
});

If you wanted to filter on the front end you can use the power and flexibility of javascript and its frameworks as the AngularJS you have commented on, to adjust your objects to your needs .

Example:

Assuming you received timestamp of your webservice and using AngularJS .

var datasTratada = datasEmTimestamp.map(function(data) {
  var date = new Date(data);
  var dataTratada = {
    formatada: $filter('date')(data, 'dd/MM/YYYY - HH:mm'),
    dia: date.getDate(),
    mes: date.getMonth(),
    ano: date.getFullYear(),
    hora: date.getHours(),
    minuto: date.getMinutes()
  }
  return dataTratada;
});

Already from a list of dates in timestamp you get a multitude of information about the dates and can work as you want. In the case of AngularJS , look at the date filter documentation for it, it's very complete: link .

A great library for working with dates in javascript is also moment.js .

    
10.02.2017 / 13:08