一,概述
- Duration表示从一个时间点到另一个时间点的时间差
- 如果是一个较晚的时间点和一个较早的时间点,Duration可能是负数
二,创建Duration
- 唯一的构造函数创建Duration对象
Duration({int days: 0, int hours: 0, int minutes: 0, int seconds: 0, int milliseconds: 0, int microseconds: 0})
- 可以使用其中的一个或者几个参数创建
- 只是用其中的一个参数
Duration ration = Duration(days: 1); print(ration); //24:00:00.000000 Duration ration1 = Duration(hours: 10); print(ration1); //10:00:00.000000
- 只是用其中的两个参数
Duration ration2 = Duration(days: 1, hours: 3); print(ration2); //27:00:00.000000
- 使用所有的参数
Duration ration3 = Duration(days: 2, hours: 2, minutes: 23, seconds: 34, milliseconds: 56, microseconds: 89); print(ration3); //50:23:34.056089
- 只是用其中的一个参数
三,相关运算
- 示例
Duration time1 = Duration(days: 1, hours: 1, minutes: 1, seconds: 1, milliseconds: 1, microseconds: 1); Duration time2 = Duration(days: 2, hours: 2, minutes: 2, seconds: 2, milliseconds: 2, microseconds: 2); print(time1); //25:01:01.001001 print(time2); //50:02:02.002002
- 加
print(time1 + time2); //75:03:03.003003
- 减
print(time1 - time2); //-25:01:01.001001
- 乘
print(time1 * 2); //50:02:02.002002
- 除(取整)
print(time2 ~/ 3); //16:40:40.667334
- 比较
print(time1 > time2); //false print(time1 >= time2); //false print(time1 == time2); //false print(time1 < time2); //true print(time1 <= time2); //true
- 取相反值
print(-time1); //-25:01:01.001001 print(-(time1 - time2)); //25:01:01.001001
- 加
四,相关函数
- 示例
Duration time3 = -Duration(days: 1, hours: 1, minutes: 1, seconds: 1, milliseconds: 1, microseconds: 1); print(time3); //-25:01:01.001001
- 取绝对值
print(time3.abs()); //25:01:01.001001
- 比较, 返回值, 0: 相等, -1: time1 < time2, 1: time1 > time2
print(time1.compareTo(time2)); //-1
- 字符串形式
print(time1.toString());
- 取绝对值