Reputation: 55
How can I change the Date format in dart
I get the from and to date difference value is int. How to change this integer to date format... and convert it to 0Days 0Months 7days; I need this type of format
but I got this type of format see the Vehicle Age:
vehicleAge(DateTime doPurchase, DateTime doRenewel) {
age = doPurchase.difference(doRenewel).abs().inDays.toInt();
return age;
}
That is my function...
Upvotes: 0
Views: 4412
Reputation: 55
Correct Answer
I got a correct answer use jiffy package.
import 'package:jiffy/jiffy.dart';
then use this code
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";
}
Upvotes: 0
Reputation: 781
Use this package time_machine and try this code
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";
}
Upvotes: 2
Reputation: 1189
Try with this
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";
}
Upvotes: 0