Advertisement
abimulya_

Date Util add DateFormat function

Oct 14th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.50 KB | None | 0 0
  1. import 'package:accurate_lite/core/config/router/router.dart';
  2. import 'package:easy_localization/easy_localization.dart';
  3. import 'package:injectable/injectable.dart';
  4. import 'package:intl/date_symbol_data_local.dart';
  5.  
  6. enum FormatDate { short, numeric, dayMonth, monthYear }
  7.  
  8. @lazySingleton
  9. class DateUtil {
  10.   DateFormat dateFormat({
  11.     FormatDate? format = FormatDate.short,
  12.   }) {
  13.     final _locale = routerNavigator.currentContext?.locale.toString();
  14.  
  15.     switch (format) {
  16.       case FormatDate.numeric:
  17.         // 14/10/2024
  18.         return DateFormat('dd/MM/yyyy', _locale);
  19.       case FormatDate.dayMonth:
  20.         // 10 Okt
  21.         return DateFormat('dd MMM', _locale);
  22.       case FormatDate.monthYear:
  23.         // Oktober 2024
  24.         return DateFormat('MMMM yyyy', _locale);
  25.       default:
  26.         // 10 Okt 2024 / 01 Okt 2024
  27.         return DateFormat('dd MMM yyyy', _locale);
  28.     }
  29.   }
  30.  
  31.   String dateFormatFormatter({
  32.     required DateTime dateTime,
  33.     FormatDate? format,
  34.   }) {
  35.     return dateFormat(
  36.       format: format,
  37.     ).format(dateTime);
  38.   }
  39.  
  40.   DateTime dateFormatParser({
  41.     required String dateTime,
  42.     FormatDate? format,
  43.   }) {
  44.     return dateFormat(
  45.       format: format,
  46.     ).parse(dateTime);
  47.   }
  48.  
  49.   String expiredDaysLeft(String? endDateLicense, DateTime? accessibleUntil) {
  50.     initializeDateFormatting();
  51.  
  52.     // final currentDate = DateTime.now().format('dd/MM/yyyy');
  53.     final currentDate = dateFormatFormatter(
  54.         dateTime: DateTime.now(), format: FormatDate.numeric);
  55.     final expiredFormatted = accessibleUntil != null
  56.         ? dateFormatFormatter(
  57.             dateTime: accessibleUntil, format: FormatDate.numeric)
  58.         : currentDate;
  59.  
  60.     final date1 =
  61.         dateFormatParser(dateTime: currentDate, format: FormatDate.numeric);
  62.     final date2 = dateFormatParser(
  63.         dateTime: expiredFormatted, format: FormatDate.numeric);
  64.     final withMinusDay = date1.subtract(const Duration(days: 1));
  65.  
  66.     return withMinusDay
  67.         .difference(date2)
  68.         .inDays
  69.         .toString()
  70.         .replaceFirst('-', '');
  71.   }
  72.  
  73.   String expiredOnDay(DateTime? accessibleUntil) {
  74.     return accessibleUntil != null
  75.         ? DateFormat('EEEE, dd/MM/yyyy', 'id_ID').format(accessibleUntil)
  76.         : '';
  77.   }
  78.  
  79.   bool isGracePeriod(String? endDate) {
  80.     bool isExpiredDate = false;
  81.     if (endDate?.isNotEmpty ?? false) {
  82.       final DateFormat _dateFormat = dateFormat(format: FormatDate.numeric);
  83.       final DateTime now = DateTime.now();
  84.       final DateTime dateNow = _dateFormat.parse(_dateFormat.format(now));
  85.       final DateTime dateEnd = _dateFormat.parse(endDate!);
  86.       // the duration to show expired time before finally cant access db
  87.       final DateTime datePeriod = dateEnd.add(const Duration(days: 8));
  88.       final bool inRange = dateNow.isAtSameMomentAs(datePeriod)
  89.           ? false
  90.           : !dateNow.isAfter(datePeriod);
  91.       final bool isExpired = dateNow.isAfter(dateEnd);
  92.       isExpiredDate = isExpired && inRange;
  93.     }
  94.     return isExpiredDate;
  95.   }
  96.  
  97.   String convertDate(
  98.     DateTime date,
  99.     bool useRelativeDate,
  100.   ) {
  101.     final now = DateTime.now();
  102.  
  103.     if (useRelativeDate) {
  104.       if (date.year == now.year &&
  105.           date.month == now.month &&
  106.           date.day == now.day) {
  107.         return 'Hari Ini';
  108.       } else {
  109.         return dateFormatFormatter(dateTime: date);
  110.       }
  111.     } else {
  112.       return dateFormatFormatter(dateTime: date, format: FormatDate.numeric);
  113.     }
  114.   }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement