Flutter:如何使用time in和time out计算总工时
Flutter: how to calculate total working hours by using time in and time out
我正在创建一个用户可以超时和超时的应用程序,我想添加计算总工作时间的功能。例如,如果时间值是 10:30,超时值是 12:30,那么总工作时间应该是 2.
请帮助我如何做到这一点。
更新:
final DateTime checkInTime = DateTime.now().subtract(Duration(hours: 9, minutes: 10));
final DateTime checkOutTime = DateTime.now().subtract(Duration(hours: 5, minutes: 10));
final hours = checkOutTime.difference(checkInTime).inHours;
final minutes= checkOutTime.difference(checkInTime).inMinutes;
final totalWorkingHours = '$hours.${(minutes - (hours*60))} hrs';
print("hours "+totalWorkingHours); //output 4.0 hrs
它给了我 4 个工作时间,但它应该 6:0
你可以用简单的数学来完成,即
超时 - 超时 = 总工作时间
让我们分解一下如何在flutter中解决这个问题
- 首先在 DateTime 中用户为
In
时保存时间戳,即 DateTime.now()
- 为
Out
重复步骤 1
- 现在如果两者都不为空计算差值
代码看起来像
DateTime inTime = populateYourInTimeHere();
DateTime outTime = poupulateYourOutTimeHere();
Duration workingHours = outTime.difference(inTime);// main part you need to know
format(Duration d) => d.toString().split('.').first.padLeft(8, "0");
The total working hour is format(workingHours)
你可以这样做
void main() {
final DateTime checkInTime = DateTime.now().subtract(Duration(hours: 3));
final DateTime checkOutTime = DateTime.now();
final totalWorkingHours = checkOutTime.difference(checkInTime).inHours;
print(totalWorkingHours);
}
输出将为 3
上面的代码只处理小时,但是如果你想同时处理小时和分钟,那么使用下面的代码
void main() {
final DateTime checkInTime = DateTime.now().subtract(Duration(hours: 3, minutes: 30));
final DateTime checkOutTime = DateTime.now();
final hours = checkOutTime.difference(checkInTime).inHours;
final minutes= checkOutTime.difference(checkInTime).inMinutes;
final totalWorkingHours = '$hours.${(minutes - (hours*60))} hrs';
print(totalWorkingHours);
}
输出将是 3.30 小时
我正在创建一个用户可以超时和超时的应用程序,我想添加计算总工作时间的功能。例如,如果时间值是 10:30,超时值是 12:30,那么总工作时间应该是 2.
请帮助我如何做到这一点。
更新:
final DateTime checkInTime = DateTime.now().subtract(Duration(hours: 9, minutes: 10));
final DateTime checkOutTime = DateTime.now().subtract(Duration(hours: 5, minutes: 10));
final hours = checkOutTime.difference(checkInTime).inHours;
final minutes= checkOutTime.difference(checkInTime).inMinutes;
final totalWorkingHours = '$hours.${(minutes - (hours*60))} hrs';
print("hours "+totalWorkingHours); //output 4.0 hrs
它给了我 4 个工作时间,但它应该 6:0
你可以用简单的数学来完成,即
超时 - 超时 = 总工作时间
让我们分解一下如何在flutter中解决这个问题
- 首先在 DateTime 中用户为
In
时保存时间戳,即 DateTime.now() - 为
Out
重复步骤 1
- 现在如果两者都不为空计算差值
代码看起来像
DateTime inTime = populateYourInTimeHere();
DateTime outTime = poupulateYourOutTimeHere();
Duration workingHours = outTime.difference(inTime);// main part you need to know
format(Duration d) => d.toString().split('.').first.padLeft(8, "0");
The total working hour is format(workingHours)
你可以这样做
void main() {
final DateTime checkInTime = DateTime.now().subtract(Duration(hours: 3));
final DateTime checkOutTime = DateTime.now();
final totalWorkingHours = checkOutTime.difference(checkInTime).inHours;
print(totalWorkingHours);
}
输出将为 3
上面的代码只处理小时,但是如果你想同时处理小时和分钟,那么使用下面的代码
void main() {
final DateTime checkInTime = DateTime.now().subtract(Duration(hours: 3, minutes: 30));
final DateTime checkOutTime = DateTime.now();
final hours = checkOutTime.difference(checkInTime).inHours;
final minutes= checkOutTime.difference(checkInTime).inMinutes;
final totalWorkingHours = '$hours.${(minutes - (hours*60))} hrs';
print(totalWorkingHours);
}
输出将是 3.30 小时