首页 > 解决方案 > 如何表示时钟

问题描述

是否有内置的方式来表示飞镖或颤振中的时钟?我只想以 24 小时制表示时间。

PS我目前正在推出自己的解决方案,因为我需要一个静态的、对语言环境不敏感的时钟,这很容易。但我只是在寻找是否有办法。

PPS我拥有后端。所以我能够调整它以返回日期。

老问题:我从一个我想在我的颤振应用程序中解析的 API 获得了时间。我看了DateTime课,但没有明显的方法来解析时间,因为我对日期不感兴趣。有没有办法只表示时间并在飞镖中解析它?Ex-20:05:00这应该意味着 8 点后 5 分钟。

PS这是我的实现

extension NumberUtil on int {
  bool isBetween(int lowerBound, int upperBound) {
    /// Checks if an int is between lower and upper bound both inclusive
    return this >= lowerBound && this <= upperBound;
  }
}

class Time {
  final int hours, minutes, seconds;

  Time(this.hours, this.minutes, this.seconds) {
    assert( hours.isBetween(0, 24) && minutes.isBetween(0, 60) && seconds.isBetween(0, 60) );
  }

  factory Time.parse(String string) {
    /// String should be in 24 hour format: hh:mm:ss. The caller is responsible
    /// for catching the parsing exception because:
    /// 1. It should almost never occur.
    /// 2. Because this is a UI app, the UI is most suited to handle that by
    /// displaying a "something went wrong" dialog

    final temp = string.split(':');

    assert (temp.length == 3);
    return Time(
      int.parse(temp[0]),
      int.parse(temp[1]),
      int.parse(temp[2]),
    );
  }

  int compareTo(Time t) {
    /// Performs a 3 way comparison between the this and t. Returns 0 if the two
    /// objects are same. Returns 1 if this is greater than t. Returns -1 otherwise

    if (this.hours > t.hours) return 1;
    if (this.hours < t.hours) return -1;

    /// at this point hours is same

    if (this.minutes > t.minutes) return 1;
    if (this.minutes < t.minutes) return -1;

    /// At this point minutes are same

    if (this.seconds > t.seconds) return 1;
    if (this.seconds < t.seconds) return -1;

    /// At this point everything is same
    return 0;
  }
}

标签: datetimedarttime

解决方案


你是这个意思吗?

currentTime = DateTime.now();
targetTime = DateTime.parse('here some times format');

currentTime.difference(targetTime)  or  currentTime.difference(targetTime).inHours // this??

或者

你找到这个包 https://pub.dev/packages/timeago


推荐阅读