I have created a C# REST WCF service which return a timespan. In client side I am getting JSON serialized return value like PT14H,PT16H etc. How to convert this string to actual timespan?
JavaScript does not have a TimeSpan
data type, but you can use moment.js.
Moment.js
supports ISO 8601 time intervals (just like .NET TimeSpan
), they're called durations
.
It includes the basic arithmetic operations: if you substract dates you get durations, if you add dates and durations you get dates, if you add or substract durations you get durations just like .NET DateTime and TimeSpan
.
Example:
var now= moment();
// 7 hour time span
var timeSpan = moment.duration('PT7H');
// addition
alert(now.add(timeSpan).format());
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
In my case JSON result looks like PT14H represent time 14:00:00 and PT9H25M25S represent time 9:25:25. That means PT represent time part, H represent hourse part, M represent minute part and S represent seconds part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With