【发布时间】:2015-05-11 03:32:18
【问题描述】:
当下面的setter接收到json字符串中的endDate="2015-05-01T00:00:00+08:00"时,时区偏移量丢失,值为2015-05-01T00:00:00。当偏移量丢失时,我需要将日期调整为 UTC。
对象在使用 JSON.NET 的 WebAPI 格式化程序中自动反序列化
private DateTime? _endDate;
public DateTime? endDate
{
get {
//...
}
set { _endDate = value; }
}
出了什么问题,或者我怎样才能获得 UTC 时间?
【问题讨论】:
-
执行 endDate.ToUniversalTime() 会得到什么?
-
如果您想使用偏移量,可以使用 DateTimeOffset 来实现此目的。你有什么理由必须使用
DateTime? -
@DWright 你是对的。偏移量是可用的,我只是在调试检查器中没有看到。
-
@JesseGood 因为数据库适配器在这种情况下(MongoDB)以不合需要的方式序列化了
DateTimeOffset。
标签: c# datetime asp.net-web-api json.net