toDate method

String toDate({
  1. String? format,
  2. String? local,
  3. TimeFormats? timeFormat,
  4. DateFormats dateFormat = DateFormats.dateDMCY,
  5. String? separator,
})

Converts the custom date to a formatted string.

The method provides flexibility for custom formatting options.

Parameters:

  • format: Custom date and time format string.
  • local: Optional parameter to specify the locale for formatting.
  • timeFormat: Enum specifying the time format (e.g., hour, minute, second).
  • dateFormat: Enum specifying the date format (e.g., day, month, year).
  • separator: Separator string between date and time components.

Example:

DateTime myDate = DateTime.now();
String formattedDate = myDate.toDate(
  dateFormat: DateFormats.dateDMCY,
  timeFormat: TimeFormats.timeHMa,
  separator: " at ",
);
print(formattedDate);  // Output: "06-02-2024 at 03:45 PM"

Implementation

String toDate({
  String? format,
  String? local,
  TimeFormats? timeFormat,
  DateFormats dateFormat = DateFormats.dateDMCY,
  String? separator,
}) {
  if ((format ?? "").isEmpty) {
    if (timeFormat.isUsable && dateFormat.isUsable) {
      format = "${dateFormat.use}'${separator ?? ' '}'${timeFormat.use}";
    } else if (timeFormat.isUsable) {
      format = timeFormat.use;
    } else {
      format = dateFormat.use;
    }
  }
  return DateFormat(format, local).format(_v);
}