datetime - 如何表示时钟
问题描述
是否有内置的方式来表示飞镖或颤振中的时钟?我只想以 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;
}
}
解决方案
你是这个意思吗?
currentTime = DateTime.now();
targetTime = DateTime.parse('here some times format');
currentTime.difference(targetTime) or currentTime.difference(targetTime).inHours // this??
或者
推荐阅读
- javascript - 如何围绕与原点不同的实体点旋转
- python - 如何从熊猫数据框中提取每小时的第一行到新数据框
- java - 为什么数据没有插入 SQLite 数据库?(房间+rx)
- javascript - 在反应应用程序中上传图像的问题
- css - 允许在移动设备上放大 div 吗?
- python - 你能从网站上刮下变化的颜色吗?
- android - PNG 徽标在 Android 5 (api v22) 上显示两次并在启动画面中失真
- matlab - 如何只允许将数字数据输入到 uitable 和文本框?
- php - 我的核心 php 应用程序中的删除操作出现错误
- java - 如何逐行打印 Gradle BuildLauncher 的输出流