What is the most appropriate format for the Localdate field in JSON?

Asked

Viewed 119 times

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 in the front-end, how to perform a grid search with Angularjs, sort, etc, which format is most suitable or the right format exists?

  • In what language is your backend?

  • The backend is in Java, but I mean the use in front-end.

1 answer

1


I think it’s ideal that the backend sends timestamp to the front and you suit the necessary.

Ordination it is better to use with timestamp because it is only a number, in the Java be the type long or of wrapper Long.

Example:

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

In case you wanted filter in front-end you can use the power and flexibility of the javascript and its frameworks as the AngularJS that you yourself commented, to adjust your objects to your needs.

Example:

Supposing you received timestamp from 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;
});

Ai of a list of dates on timestamp you get a plethora of information about the dates and can work as you wish. In the case of AngularJS, a look at the documentation of its date filter, it is very complete: https://docs.angularjs.org/api/ng/filter/date.

A great library to work with dates in javascript is also the Moment.js.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.