Last active
April 30, 2019 19:14
-
-
Save gildaswise/b5ef419bc9a5a6ab5f66947d9c380515 to your computer and use it in GitHub Desktop.
An implementation of formatDuration in Dart, probably needs optimizations but it works and has support for changing the strings; getDateDifference by @app-o-matix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:collection/collection.dart'; | |
Function listEquals = const ListEquality().equals; | |
class DateUtils { | |
static String formatDuration(DateTime dateTime, | |
{String years = "year(s)", | |
String months = "month(s)", | |
String days = "day(s)", | |
String and = "and",}) { | |
final difference = getDateDifference(dateTime, DateTime.now()); | |
final Map<int, int> valid = {}; | |
for (var i = 0; i < difference.length; i++) { | |
valid[i] = difference[i]; | |
} | |
valid.removeWhere((key, value) => value == 0); | |
if (valid.length == difference.length) | |
return "${valid[0]} $years, ${valid[1]} $months $and ${valid[2]} $days"; | |
else { | |
final List<int> keys = valid.keys.toList(); | |
if (listEquals(keys, [0, 1])) | |
return "${valid[0]} $years $and ${valid[1]} $months"; | |
else if (listEquals(keys, [0, 2])) | |
return "${valid[0]} $years $and ${valid[2]} $days"; | |
else if (listEquals(keys, [1, 2])) | |
return "${valid[1]} $months $and ${valid[2]} $days"; | |
else if (listEquals(keys, [0])) | |
return "${valid[0]} $years"; | |
else if (listEquals(keys, [1])) | |
return "${valid[1]} $months"; | |
else if (listEquals(keys, [2])) | |
return "${valid[2]} $days"; | |
else | |
return ""; | |
} | |
} | |
static List<int> getDateDifference( | |
DateTime initialDate, DateTime subsequentDate) { | |
// Convert initialDate DateTime value to a a List<int> represent [year, month, day] | |
List<int> _initialDateSplit = [ | |
initialDate.year, | |
initialDate.month, | |
initialDate.day | |
]; | |
// Convert subsequentDate DateTime value to a a List<int> represent [year, month, day] | |
List<int> _subsequentDateSplit = [ | |
subsequentDate.year, | |
subsequentDate.month, | |
subsequentDate.day | |
]; | |
List<int> _durationBetween = []; | |
print( | |
"Initial Date - year: ${_initialDateSplit[0]}, month: ${_initialDateSplit[1]}, day: ${_initialDateSplit[2]}"); | |
print( | |
"Subsequent Date - year: ${_subsequentDateSplit[0]}, month: ${_subsequentDateSplit[1]}, day: ${_subsequentDateSplit[2]}"); | |
// Calculate year difference | |
_durationBetween.add(_subsequentDateSplit[0] - _initialDateSplit[0]); | |
// Calculate month difference | |
if (_initialDateSplit[1] <= _subsequentDateSplit[1]) { | |
_durationBetween.add(_subsequentDateSplit[1] - _initialDateSplit[1]); | |
// Adjust year difference if initial month is after subsequent month in the calendar | |
} else { | |
_durationBetween.add(12 - _initialDateSplit[1] + _subsequentDateSplit[1]); | |
--_durationBetween[0]; | |
} | |
// Calculate day difference | |
if (_initialDateSplit[2] <= _subsequentDateSplit[2]) { | |
_durationBetween.add(_subsequentDateSplit[2] - _initialDateSplit[2]); | |
// Adjust month difference if initial day is after subsequent day as a number | |
// and the initial month is a 31 day month | |
} else if ([1, 3, 5, 7, 8, 10, 12].contains(_initialDateSplit[1])) { | |
_durationBetween.add(31 - _initialDateSplit[2] + _subsequentDateSplit[2]); | |
--_durationBetween[1]; | |
// Adjust month difference if initial day is after subsequent day as a number | |
// and the initial month is a 30 day month | |
} else if ([4, 6, 9, 11].contains(_initialDateSplit[1])) { | |
_durationBetween.add(30 - _initialDateSplit[2] + _subsequentDateSplit[2]); | |
--_durationBetween[1]; | |
// Adjust month difference if initial day is after subsequent day as a number | |
// and the initial month is February and the initial year is a leap year | |
} else if ((_initialDateSplit[0] % 4 == 0 && | |
_initialDateSplit[0] % 100 != 0) || | |
_initialDateSplit[0] % 400 == 0) { | |
_durationBetween.add(29 - _initialDateSplit[2] + _subsequentDateSplit[2]); | |
--_durationBetween[1]; | |
// Adjust month difference if initial day is after subsequent day as a number | |
// and the initial month is February and the initial year is not a leap year | |
} else { | |
_durationBetween.add(28 - _initialDateSplit[2] + _subsequentDateSplit[2]); | |
--_durationBetween[1]; | |
} | |
return _durationBetween.toList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment