One of the challenges I'm facing involves a REST API that sends back JSON data with dates formatted in ISO-8601 style: yyyy-MM-ddTHH:mm:ss:
{
id: 4
version: 3
code: "ADSFASDF"
definition: "asdflkj"
type: "CONTAINER"
value: "1234"
active: "false"
formula: false
validTo: "2014-12-31T05:00:00"
validFrom: "2010-12-31T10:00:00"
}
My dilemma lies in handling this within AngularJS. I use a $resource
for interacting with my API endpoints, but when the data is returned, it is stored as a String in my JavaScript object. I believe it would be more convenient to manage these dates as Date() or Moment() objects.
In Java, a JsonDeserializer
can be used to ensure proper conversion of all JSON data before assigning it to the model. However, I'm unsure if there's an equivalent mechanism in AngularJS.
Although I've done some research, I haven't found a generalized solution to implement. While utilizing a transformResponse
function in my $resource
is an option, it seems like repetitive configuration for each date-containing data type.
This situation prompts me to question whether returning dates in ISO-8601 format is the most effective approach. If AngularJS lacks native support, there must be a simpler way to handle dates. How do you handle dates in AngularJS? Should they be treated purely as text/string objects and have the API return a pre-formatted version? If so, what is the optimal format for flexibility in an HTML5 date input box, among other uses?