Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:accurate_lite/core/config/router/router.dart';
- import 'package:easy_localization/easy_localization.dart';
- import 'package:injectable/injectable.dart';
- import 'package:intl/date_symbol_data_local.dart';
- enum FormatDate { short, numeric, dayMonth, monthYear }
- @lazySingleton
- class DateUtil {
- DateFormat dateFormat({
- FormatDate? format = FormatDate.short,
- }) {
- final _locale = routerNavigator.currentContext?.locale.toString();
- switch (format) {
- case FormatDate.numeric:
- // 14/10/2024
- return DateFormat('dd/MM/yyyy', _locale);
- case FormatDate.dayMonth:
- // 10 Okt
- return DateFormat('dd MMM', _locale);
- case FormatDate.monthYear:
- // Oktober 2024
- return DateFormat('MMMM yyyy', _locale);
- default:
- // 10 Okt 2024 / 01 Okt 2024
- return DateFormat('dd MMM yyyy', _locale);
- }
- }
- String dateFormatFormatter({
- required DateTime dateTime,
- FormatDate? format,
- }) {
- return dateFormat(
- format: format,
- ).format(dateTime);
- }
- DateTime dateFormatParser({
- required String dateTime,
- FormatDate? format,
- }) {
- return dateFormat(
- format: format,
- ).parse(dateTime);
- }
- String expiredDaysLeft(String? endDateLicense, DateTime? accessibleUntil) {
- initializeDateFormatting();
- // final currentDate = DateTime.now().format('dd/MM/yyyy');
- final currentDate = dateFormatFormatter(
- dateTime: DateTime.now(), format: FormatDate.numeric);
- final expiredFormatted = accessibleUntil != null
- ? dateFormatFormatter(
- dateTime: accessibleUntil, format: FormatDate.numeric)
- : currentDate;
- final date1 =
- dateFormatParser(dateTime: currentDate, format: FormatDate.numeric);
- final date2 = dateFormatParser(
- dateTime: expiredFormatted, format: FormatDate.numeric);
- final withMinusDay = date1.subtract(const Duration(days: 1));
- return withMinusDay
- .difference(date2)
- .inDays
- .toString()
- .replaceFirst('-', '');
- }
- String expiredOnDay(DateTime? accessibleUntil) {
- return accessibleUntil != null
- ? DateFormat('EEEE, dd/MM/yyyy', 'id_ID').format(accessibleUntil)
- : '';
- }
- bool isGracePeriod(String? endDate) {
- bool isExpiredDate = false;
- if (endDate?.isNotEmpty ?? false) {
- final DateFormat _dateFormat = dateFormat(format: FormatDate.numeric);
- final DateTime now = DateTime.now();
- final DateTime dateNow = _dateFormat.parse(_dateFormat.format(now));
- final DateTime dateEnd = _dateFormat.parse(endDate!);
- // the duration to show expired time before finally cant access db
- final DateTime datePeriod = dateEnd.add(const Duration(days: 8));
- final bool inRange = dateNow.isAtSameMomentAs(datePeriod)
- ? false
- : !dateNow.isAfter(datePeriod);
- final bool isExpired = dateNow.isAfter(dateEnd);
- isExpiredDate = isExpired && inRange;
- }
- return isExpiredDate;
- }
- String convertDate(
- DateTime date,
- bool useRelativeDate,
- ) {
- final now = DateTime.now();
- if (useRelativeDate) {
- if (date.year == now.year &&
- date.month == now.month &&
- date.day == now.day) {
- return 'Hari Ini';
- } else {
- return dateFormatFormatter(dateTime: date);
- }
- } else {
- return dateFormatFormatter(dateTime: date, format: FormatDate.numeric);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement