我在将我的DateTime
识别为UTC时间时感到不安。通过http请求,我将一些数据存储在包含UTC DateTime
的SQL Server数据库中。这是通过实体框架完成的。
var workingTime = new WorkingTime()
{
StartDateTime = dto.StartDateTime.ToUniversalTime(),
EndDateTime = dto.EndDateTime.ToUniversalTime(),
...
};
await _repository.AddAsync(workingTime);
这看起来很安静,请求是在德国的2020-07-13 01-59-41:690
完成的,所以存储的数据是正确的UTC时间。
现在,我正在将这些数据加载到我的Flutter应用程序中,如果我调试了我的应用程序,则转换后的datetime
表示这不是UTC时间。
我不确定,如果我存储的数据错误,或者我在flutter中的解析错误,但是在这里看不到我做错了。
请告诉我您是否需要代码或更多信息。
每次都要明确说明UTC。向编译器指示您要使用UTC时间。要获得毫秒数,因为UTC时间中的Encech:
DateTime dateTime=DateTime.now().toUtc(); int epochTime = dateTime.toUtc().millisecondsSinceEpoch;
要获取UTC的日期时间:
DateTime dt=DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch).toUtc();