本文介绍了如何获得2次之间的差额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Flutter应用程序,作为一个项目,我坚持如何获得两次之间的差异。我得到的第一个是从Firebase作为字符串,然后使用以下命令将其格式化为DateTime: DateTime.parse(snapshot.documents [i] .data ['from']),例如,它给了我 14:00 。然后,第二个是 DateTime.now()
我尝试了所有方法差异减去,但是没有任何效果!

I'm working on a flutter app as a project and I'm stuck with how to get the difference between two times. The first one I'm getting is from firebase as a String, which I then format to a DateTime using this:DateTime.parse(snapshot.documents[i].data['from']) and it gives me 14:00 for example. Then, the second is DateTime.now().I tried all methods difference, subtract, but nothing works!

请帮助我获取这两次之间的确切持续时间。
我需要一个倒计时计时器。

Please help me to get the exact duration between those 2 times.I need this for a Count Down Timer.

这是我的代码概述:

.......

class _ActualPositionState extends State<ActualPosition>
    with TickerProviderStateMixin {
  AnimationController controller;
  bool hide = true;
  var doc;

  String get timerString {
    Duration duration = controller.duration * controller.value;
    return '${duration.inHours}:${duration.inMinutes % 60}:${(duration.inSeconds % 60).toString().padLeft(2, '0')}';
  }

  @override
  void initState() {
    super.initState();
    var d = Firestore.instance
        .collection('users')
        .document(widget.uid);
    d.get().then((d) {
      if (d.data['parking']) {
        setState(() {
          hide = false;
        });
        Firestore.instance
            .collection('historyParks')
            .where('idUser', isEqualTo: widget.uid)
            .getDocuments()
            .then((QuerySnapshot snapshot) {
          if (snapshot.documents.length == 1) {
            for (var i = 0; i < snapshot.documents.length; i++) {
              if (snapshot.documents[i].data['date'] ==
                  DateFormat('EEE d MMM').format(DateTime.now())) {
                setState(() {
                  doc = snapshot.documents[i].data;
                });
                Duration t = DateTime.parse(snapshot.documents[i].data['until'])
                    .difference(DateTime.parse(
                        DateFormat("H:m:s").format(DateTime.now())));

                print(t);
              }
            }
          }
        });
      }
    });
    controller = AnimationController(
      duration: Duration(hours: 1, seconds: 10),
      vsync: this,
    );
    controller.reverse(from: controller.value == 0.0 ? 1.0 : controller.value);
  }

  double screenHeight;
  @override
  Widget build(BuildContext context) {
    screenHeight = MediaQuery.of(context).size.height;
    return Scaffold(

.............


推荐答案

您可以使用以下方法找到to时间之间的时差:

you can find the difference between to times by using:

诸如此类:

var startTime = DateTime(2020, 02, 20, 10, 30); // TODO: change this to your DateTime from firebase
var currentTime = DateTime.now();
var diff = currentTime.difference(startTime).inDays; // HINT: you can use .inDays, inHours, .inMinutes or .inSeconds according to your need.

DartPad中的示例:

example from DartPad:

void main() {

final startTime = DateTime(2020, 02, 20, 10, 30);
final currentTime = DateTime.now();

final diff_dy = currentTime.difference(startTime).inDays;
final diff_hr = currentTime.difference(startTime).inHours;
final diff_mn = currentTime.difference(startTime).inMinutes;
final diff_sc = currentTime.difference(startTime).inSeconds;

print(diff_dy);
print(diff_hr);
print(diff_mn);
print(diff_sc);

}

希望这有帮助!!

这篇关于如何获得2次之间的差额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 05:00