One unique aspect of TypeScript is that it does not have a specific "deserialization step". In TypeScript, the DTOs simply define the type that is returned in the raw JSON. Since JSON does not have a data type for dates, the date values are returned as strings. When converting these strings into JavaScript objects using either JSON.parse()
or eval()
, the date remains a string.
To convert the default WCF Date format used by ServiceStack.Text, you can use the following function:
function todate (s) {
return new Date(parseFloat(/Date\(([^)]+)\)/.exec(s)[1]));
};
If you are utilizing the servicestack-client npm package, you can resolve the conversion like this:
import { todate } from "servicestack-client";
var date = todate(wcfDateString);
Alternatively, if you are using ss-utils.js provided by ServiceStack:
var date = $.ss.todate(wcfDateString);
By changing the default serialization of dates in ServiceStack.Text to ISO8601 format:
JsConfig.DateHandler = DateHandler.ISO8601;
You can then parse the dates natively with:
new Date(dateString)
Similarly, configuring ServiceStack.Text to return dates in UnixTimeMs format:
JsConfig.DateHandler = DateHandler.UnixTimeMs;
Will allow you to convert them natively using:
new Date(unixTimeMs)