我收到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)
编辑:
date是DateTime我从 API 收到的。这是在UTC时区。
我使用过print('TIMEZONENAME: ' + date.timeZoneName;,它会自动将时区设置为HKT。这就是为什么当我尝试使用它时它什么也不做date.toLocal()