I have an ODATA node, which automatically returns data in Json format.
One of the properties on this object contains an ISO 8601 time span , as it is generated using TimeSpan
(from .Net)
For example, a span of 7 hours is serialized like this:
"PT7H"
I consume this API from a client in AngularJs and I need to add and subtract hours and minutes, and JavaScript doesn't have a native TimeSpan data type.
You JavaScript ninjas, how do you handle this kind of situation? That is, how do I convert that format to a "friendly" JavaScript object that I can use?
Which bookstore is recommended? moment.js? date.js?
Here are some pitiful attempts of mine:
var resultado=JSON.parse(valorTS); //mismo valor
var resultado=Date(valorTS); //Invalid date
(Same question: https://stackoverflow.com/questions/33893265/how-to-deserialize-json-timespan-in-javascript )
Moment.js supports ISO 8601 time spans (just like C#'s TimeSpan), it calls them durations . Includes basic operations: add and subtract. If you subtract dates you get durations, if you add dates and durations you get dates, if you add/subtract durations you get durations, if you add dates... you can't, just like C#'s DateTime and TimeSpan.
If I were you, I wouldn't spend valuable time developing something that is done and well done, like Moment.js -unless it's for learning purposes-.
Why not take advantage of the situation to create a bookstore?
If what you want is to be able to add and subtract these values, either between them and with dates, you only need two functions (addition and subtraction).