【发布时间】:2020-10-17 11:23:02
【问题描述】:
我正在尝试在我的数据库中保存可变长度的时间。最长的可能包括几个小时,较短的只有几分钟。较短的字符串只会读取“mm:ss.ms”或“:ss.ms”,因此总长度会更短。
我试过DateTime,但它需要年、月和日。我尝试将日期设置为 0000-00-00,但没有成功。
我尝试对所有内容进行硬编码,但希望有更优雅的解决方案。这是我走了多远:
// for shorter string of type ":mm.ms"
if (myString.length >= 3) AND (myString.length <= 4) {
if (myString.contains(':')) {
myString.substring(1); //remove initial semicolon
}
int _seconds = int.parse(myString.substring(0,1));
int _milliseconds = int.parse(myString.substring(3,3));
}
任何指针将不胜感激。
【问题讨论】:
-
使用
Duration。
标签: datetime flutter dart converters stopwatch