最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How To Remove Microseconds Portion Of DateTime From ServiceStack Serialized Response? - Stack Overflow

programmeradmin0浏览0评论

I am using ServiceStack v8.5.2 and have configured the date handler as follows:-

JsConfig.DateHandler = DateHandler.ISO8601;

I have a response POCO which has a DateTime field and the serialized output is:-

"created": "2025-02-05T09:49:25.0000000Z"

I have a lot of DateTime fields and they all have no microsecond value so the response is wasting a lot of bandwidth in sending back .000000 for every item.

I have not been able to find a simple way to customise the date handler format to remove the microseconds portion, most of the hits I have read through are from 10 or more years ago and don't seem to work with this current version. I feel like I am missing something really obvious as it does not seem like a major thing to change - can anyone help?

I am using ServiceStack v8.5.2 and have configured the date handler as follows:-

JsConfig.DateHandler = DateHandler.ISO8601;

I have a response POCO which has a DateTime field and the serialized output is:-

"created": "2025-02-05T09:49:25.0000000Z"

I have a lot of DateTime fields and they all have no microsecond value so the response is wasting a lot of bandwidth in sending back .000000 for every item.

I have not been able to find a simple way to customise the date handler format to remove the microseconds portion, most of the hits I have read through are from 10 or more years ago and don't seem to work with this current version. I feel like I am missing something really obvious as it does not seem like a major thing to change - can anyone help?

Share Improve this question asked Feb 5 at 16:48 danrockcolldanrockcoll 2554 silver badges13 bronze badges 3
  • I have a lot of DateTime fields and they all have no microsecond value so the response is wasting a lot of bandwidth in sending back .000000 for every item: When you calculated the wasted bandwidth what was your result? Also, during your testing what was the average amount of time delay that this wasted bandwidth caused? Please add the calculations to your post. – It all makes cents Commented Feb 5 at 18:15
  • DateTime precision is a tick which is a 100ns. So to truncate you can divide and then multiply to drop the least significant values. So integer divide by 1000 and then multiply by 1000 will truncate 4 decimal places. You can instead use a custom DateTime format : "yyyy-MM-ddTHH:mm:ss.fff" – jdweng Commented Feb 5 at 22:36
  • with the microseconds removed, i get about a 5% reduction in the packet size. this change is being done in conjunction with a number of other ones to minimise the response as much as possible coz it is being sent to a client over a satellite link which can be as little as 125kb/s – danrockcoll Commented Feb 6 at 10:36
Add a comment  | 

1 Answer 1

Reset to default 2

Here are docs for JSON configuration options. E.g. for a more succinct format you can use:

JsConfig.DateHandler = DateHandler.ISO8601DateTime;

Or you can control the serialization of a built-in Type with a custom Serialize function:

JsConfig<DateTime>.SerializeFn = o => 
    o.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture);
发布评论

评论列表(0)

  1. 暂无评论