如何计算两个日期之间的差异,并在颤振中得到年月日



如何更改dart中的日期格式

我得到从和到日期的差值是int。如何将此整数更改为日期格式。。。并将其转换为0天0个月7天;我需要这种格式的

但我有这种格式参见车辆年龄:

vehicleAge(DateTime doPurchase, DateTime doRenewel) {
age = doPurchase.difference(doRenewel).abs().inDays.toInt();
return age;
}

这是我的职责。。。

使用此软件包time_machine并尝试此代码

vehicleAge(DateTime doPurchase, DateTime doRenewel) {
LocalDate a = LocalDate.dateTime(doPurchase);
LocalDate b = LocalDate.dateTime(doRenewel);

Period diff = b.periodSince(a);
return "${diff.years} Years ${diff.months} Months ${diff.days} Days";
}

尝试使用此

vehicleAge(DateTime doPurchase, DateTime doRenewel) {
Duration parse = doPurchase.difference(doRenewel).abs();
return "${parse.inDays~/360} Years ${((parse.inDays%360)~/30)} Month ${(parse.inDays%360)%30} Days";
}

正确答案

我用jiffy包得到了正确答案。

import 'package:jiffy/jiffy.dart';

然后使用这个代码

vehicleAge(DateTime doPurchase, DateTime doRenewel) {
var dt1 = Jiffy(doPurchase);
var dt2 = Jiffy(doRenewel);
int years = int.parse("${dt2.diff(dt1, Units.YEAR)}");
dt1.add(years: years);
int month = int.parse("${dt2.diff(dt1, Units.MONTH)}");
dt1.add(months: month);
var days = dt2.diff(dt1, Units.DAY);

return "$years Years $month Month $days Days";

}

最新更新