convertToCurrentTimeZone static method

DateTime convertToCurrentTimeZone(
  1. DateTime dateTime,
  2. String fromTimeZone,
  3. String toTimeZone
)

Implementation

static DateTime convertToCurrentTimeZone(
    DateTime dateTime, String fromTimeZone, String toTimeZone) {
  // Parse the input datetime with the source timezone
  DateTime parsedDateTime = DateFormat('yyyy-MM-dd HH:mm:ss')
      .parse(dateTime.toString(), true)
      .toLocal();

  // Convert the datetime to the target timezone
  DateTime convertedDateTime = parsedDateTime.toUtc().add(Duration(
      hours:
      getTimeZoneOffset(toTimeZone) - getTimeZoneOffset(fromTimeZone)));

  return convertedDateTime;
}