gpt4 book ai didi

flutter - 将时间戳差异转换为年、月和周

转载 作者:行者123 更新时间:2023-12-04 03:40:32 27 4
gpt4 key购买 nike

我想在下面的代码中添加尽可能多的选项,以显示经过了多少时间。不是我有小时:分钟:秒......我在想年:月:周:小时:分钟:秒。这样的事情可能吗?

final currentTimestamp = Timestamp.now();
final timestamp = currentTimestamp.seconds;
final firestoreTimestamp = widget.timestamp;

final diff = timestamp - firestoreTimestamp.seconds;

timerSubscription = timerStream.listen((int newTick) {
if (!mounted) return;
setState(() {
hoursStr = (((newTick + diff) / (60 * 60)) % 60)
.floor()
.toString()
.padLeft(2, '0');
minutesStr =
(((newTick + diff) / 60) % 60).floor().toString().padLeft(2, '0');
secondsStr = ((newTick + diff) % 60).floor().toString().padLeft(2, '0');
});
})

最佳答案

我推荐time_machine包来实现这一点:

import 'package:time_machine/time_machine.dart';

void main() {
final start = LocalDateTime(2012, 1, 1, 14, 0, 0);
final end = LocalDateTime(2013, 3, 9, 16, 3, 4);

final period = Period.differenceBetweenDateTime(start, end, PeriodUnits.allUnits);

print("years: ${period.years}");
print("months: ${period.months}");
print("weeks: ${period.weeks}");
print("days: ${period.days}");
print("hours: ${period.hours}");
print("minutes: ${period.minutes}");
print("seconds: ${period.seconds}");
}

关于flutter - 将时间戳差异转换为年、月和周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66127529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com