相关疑难解决方法(0)

UTC 中的日期时间未转换为本地

我收到DateTime来自 API的响应,该响应将时区设置为 UTC。

但是当我尝试使用toLocal()它转换接收到的数据时,它不会转换。

我的当地时间是香港时间

这是我的代码。

    //TIME DIFFERENCE
  getNotificationDate(DateTime date) {
    date = date.toUtc();
    final convertedDate = date.toLocal();

    final dateNow = DateTime.now();
    print('TIMENOW: ' + dateNow.toString());
    print('TIMENOTIFC: ' + convertedDate.toString());
    final difference = dateNow.difference(convertedDate);
    print('DIFFERENCE: ' + difference.toString());
    return getDurationFormat(difference);
  }
Run Code Online (Sandbox Code Playgroud)

编辑:

dateDateTime我从 API 收到的。这是在UTC时区。

我使用过print('TIMEZONENAME: ' + date.timeZoneName;,它会自动将时区设置为HKT。这就是为什么当我尝试使用它时它什么也不做date.toLocal()

datetime dart flutter

18
推荐指数
5
解决办法
2万
查看次数

标签 统计

dart ×1

datetime ×1

flutter ×1