SQL Server数据库中的utc时间不正确

问题描述 投票:0回答:1

我在将我的DateTime识别为UTC时间时感到不安。通过http请求,我将一些数据存储在包含UTC DateTime的SQL Server数据库中。这是通过实体框架完成的。

var workingTime = new WorkingTime()
{
    StartDateTime = dto.StartDateTime.ToUniversalTime(),
    EndDateTime = dto.EndDateTime.ToUniversalTime(),
    ...
};

await _repository.AddAsync(workingTime);

enter image description here

这看起来很安静,请求是在德国的2020-07-13 01-59-41:690完成的,所以存储的数据是正确的UTC时间。

现在,我正在将这些数据加载到我的Flutter应用程序中,如果我调试了我的应用程序,则转换后的datetime表示这不是UTC时间。

enter image description here

我不确定,如果我存储的数据错误,或者我在flutter中的解析错误,但是在这里看不到我做错了。

请告诉我您是否需要代码或更多信息。

sql-server datetime flutter utc
1个回答
0
投票

每次都要明确说明UTC。向编译器指示您要使用UTC时间。要获得毫秒数,因为UTC时间中的Encech:

DateTime dateTime=DateTime.now().toUtc();
int epochTime = dateTime.toUtc().millisecondsSinceEpoch;

要获取UTC的日期时间:

DateTime dt=DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch).toUtc();
    
© www.soinside.com 2019 - 2024. All rights reserved.