convertToDate method

DateTime convertToDate({
  1. bool isUtc = true,
  2. DateTime? date,
})

Implementation

DateTime convertToDate({bool isUtc = true, DateTime? date}) {
  DateTime convertedDate = date ?? this;
  // First create a date-only version (midnight) in local time to avoid day shift
  DateTime localMidnight = DateTime(
    convertedDate.year,
    convertedDate.month,
    convertedDate.day,
  );

  // Convert to UTC by default, but preserve the same date
  // (not using toUtc() directly to avoid timezone shifts at midnight)
  if (isUtc) {
    return DateTime.utc(
      localMidnight.year,
      localMidnight.month,
      localMidnight.day,
    );
  }

  return localMidnight;
}