import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; class DateFormatter { static final dateFormatter = DateFormat("dd-MM-yyyy"); static final dateFormatter2 = DateFormat("dd/MM/yyyy"); String todayTomorrowYesterday(DateTime dateToCheck) { final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); final yesterday = DateTime(now.year, now.month, now.day - 1); final tomorrow = DateTime(now.year, now.month, now.day + 1); final aDate = DateTime(dateToCheck.year, dateToCheck.month, dateToCheck.day); if (aDate == today) { return 'Today'; } else if (aDate == yesterday) { return 'Yesterday'; } else if (aDate == tomorrow) { return 'Tomorrow'; } else { return 'nothing'; } } // String getRotaHeadingDate(DateTime dateTime) { // String wordDate = todayTomorrowYesterday(dateTime); // String date = DateFormat("yMMMMd").format(dateTime); // return wordDate != 'nothing' // ? "$wordDate, ${date.split(" ").first.substring(0, 3)} ${dateTime.day}" // : "${date.split(" ").first.substring(0, 3)} ${dateTime.day}"; // } String getRotaDate(DateTime dateTime) { //Nov 1, 5AM-0PM String date = DateFormat("yMMMMd").format(dateTime); return "${date.split(" ").first.substring(0, 3)} ${dateTime.day}, ${dateTime.hour}AM-${dateTime.minute}PM"; } DateTime? time24to12formatDate({required String time}) { try { if (time.isEmpty) { return null; } final f = DateFormat("HH:mm"); return f.parse(time); } catch (e) { debugPrint(e.toString()); return null; } } TimeOfDay? time24to12format({required String time}) { final dateTime = time24to12formatDate(time: time); return (dateTime == null) ? null : TimeOfDay.fromDateTime(dateTime); } String roasterShiftFormattedTime({required String time}) { final dateTime = time24to12formatDate(time: time); return (dateTime == null) ? "" : DateFormat("hh:mm aa").format(dateTime); } String getRotaNewDate( {required int shiftDate, required String startShiftTime, required String endShiftTime}) { if (shiftDate == 0 || startShiftTime.isEmpty || endShiftTime.isEmpty) { return ""; } final f = DateFormat("HH:mm"); final sd = f.parse(startShiftTime); final ed = f.parse(endShiftTime); final date = DateTime.fromMillisecondsSinceEpoch(shiftDate); return "${DateFormat("MMM dd").format(date)}, ${DateFormat("hha").format(sd)} - ${DateFormat("hha").format(ed)}"; } String getAppointmentTime(DateTime dateTime) { //Nov 1 String date = DateFormat("yMMMMd").format(dateTime); return "${date.split(" ").first.substring(0, 3)} ${dateTime.day}"; } String getHolidayDate(DateTime dateTime) { //jan 2, 2024 String date = DateFormat("MMM dd, yyyy").format(dateTime); return "${date.split(" ").first.substring(0, 3)} ${dateTime.day}, ${dateTime.year}"; } String getFormattedDateFromUtc(String utcTime) { try { return DateFormat("yyyy-MM-dd") .parse(utcTime, true) .toLocal() .toString() .split(" ") .first; //String 2024-02-22 } catch (e) { return ""; } } static String ddMMyyyyhhmmFormat(DateTime date) { return DateFormat("dd/MM/yyyy hh:mm aa").format(date); } }