import 'package:intl/intl.dart'; class DateUtils { static String convertStringToDateFormat(String date, String dateFormat) { DateTime dateTime = DateTime.parse(date); return DateFormat(dateFormat).format(dateTime); } static convertDateFromServerFormat(String str, dateFormat) { const start = "/Date("; const end = "+0300)"; final startIndex = str.indexOf(start); final endIndex = str.indexOf(end, startIndex + start.length); var date = new DateTime.fromMillisecondsSinceEpoch( int.parse(str.substring(startIndex + start.length, endIndex))); return DateFormat(dateFormat).format(date); } static String differenceBetweenDateAndCurrentInYearMonthDay(DateTime firstDate) { DateTime now = DateTime.now(); // now = now.add(Duration(days: 400, minutes: 0)); var difference = firstDate.difference(now); int years = now.year - firstDate.year; int months = now.month - firstDate.month; int days = now.day - firstDate.day; if (months < 0 || (months == 0 && days < 0)) { years--; months += (days < 0 ? 11 : 12); } if (days < 0) { final monthAgo = new DateTime(now.year, now.month - 1, firstDate.day); days = now.difference(monthAgo).inDays + 1; } return "$days Days, $months Months, $years Years"; } static String differenceBetweenDateAndCurrent(DateTime firstDate) { DateTime now = DateTime.now(); // DateTime now = nows.add(Duration(days: 400, minutes: 25, hours: 0)); var difference = now.difference(firstDate); int minutesInDays = difference.inMinutes; int hoursInDays = minutesInDays ~/ 60; int minutes = minutesInDays % 60; int days = hoursInDays ~/ 24; int hours = hoursInDays % 24; double hoursInOneDay = difference.inHours / difference.inDays; return "$days Days, $hours HR, $minutes Min"; } static String differenceBetweenServerDateAndCurrent(String str) { const start = "/Date("; const end = "+0300)"; final startIndex = str.indexOf(start); final endIndex = str.indexOf(end, startIndex + start.length); var date = new DateTime.fromMillisecondsSinceEpoch( int.parse(str.substring(startIndex + start.length, endIndex))); return differenceBetweenDateAndCurrent(date); } }