toIndiaTimeZone method

DateTime? toIndiaTimeZone()

Converts the DateTime object to the Indian time zone.

Returns a new DateTime object representing the time in the Indian time zone, or null if the input DateTime is null.

Example:

DateTime utcDateTime = DateTime.now().toUtc();
DateTime? indianDateTime = utcDateTime.toIndiaTimeZone();
print('Indian DateTime: $indianDateTime');

Implementation

DateTime? toIndiaTimeZone() {
  DateTime currentTime = this;
  if (!(isUtc)) {
    currentTime = toUtc();
  }
  // Define the time difference between UTC and IST
  const int istOffsetMinutes = 330; // UTC+5:30
  // Calculate the time difference in milliseconds
  int timeDifferenceMilliseconds = istOffsetMinutes * 60 * 1000;
  // Apply the time difference to the server time
  DateTime istDateTime =
      currentTime.add(Duration(milliseconds: timeDifferenceMilliseconds));
  final iSTDate =
      istDateTime.toIso8601String().replaceAll('Z', '').toDateTime;
  return iSTDate;
}