Flutter Difference in seconds between two timestamps

要获取两个时间戳之间的秒数差异,可以使用以下代码:

```dart
DateTime timestamp1 = DateTime.fromMillisecondsSinceEpoch(1609459200000); // 第一个时间戳,单位为毫秒
DateTime timestamp2 = DateTime.fromMillisecondsSinceEpoch(1609462800000); // 第二个时间戳,单位为毫秒

Duration difference = timestamp2.difference(timestamp1);
int seconds = difference.inSeconds;

print(seconds); // 输出相差的秒数

In this example, we first convert the two timestamps intoDateTime objects. We then use the difference method to get an Duration object between two times that contains the amount of time that differs. Finally, we use the inSeconds attribute to get the number of seconds apart.


Guess you like

Origin blog.csdn.net/weixin_44911775/article/details/134049733