Skip to content

Instantly share code, notes, and snippets.

@apgapg
Last active November 10, 2021 16:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apgapg/84d855e41c0134a34ff8b2cf034ad249 to your computer and use it in GitHub Desktop.
Save apgapg/84d855e41c0134a34ff8b2cf034ad249 to your computer and use it in GitHub Desktop.
Get correct ISO DateTime String with offset in Flutter: https://dartpad.dev/84d855e41c0134a34ff8b2cf034ad249
import 'package:intl/intl.dart';
import 'package:meta/meta.dart';
void main() {
print(DateUtils.formatISOTime(DateTime.now()));
print(DateUtils.getCurrentISOTimeString());
}
class DateUtils {
///Converts DateTime to ISO format
///Output: 2020-09-16T20:41:09.331+05:30
static String formatISOTime(DateTime date) {
var duration = date.timeZoneOffset;
if (duration.isNegative)
return (date.toIso8601String() +
"-${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
else
return (date.toIso8601String() +
"+${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}
///get ISO time String from DateTime
///Output: 2020-09-16T20:42:38.629+05:30
static String getCurrentISOTimeString({DateTime dateTime}) {
var date = dateTime ?? DateTime.now();
//Time zone may be null in dateTime hence get timezone by datetime
var duration = DateTime.now().timeZoneOffset;
if (duration.isNegative)
//TODO: convert duration to abs value instead of below params
return (date.toIso8601String() +
"-${duration.inHours.abs().toString().padLeft(2, '0')}:${(duration.inMinutes.abs() - (duration.inHours.abs() * 60)).toString().padLeft(2, '0')}");
else
return (date.toIso8601String() +
"+${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}
}
@adimar-aot
Copy link

Thank you for sharing.
I did few changes on it:

class DateUtils {
 
 ///Converts DateTime to ISO format 
 ///Output: 2020-09-16T20:41:09.331+05:30
  static String formatISOTime(DateTime date) {
    var duration = date.timeZoneOffset;
    
    return "${date.toIso8601String().replaceAll('Z', '')}${duration.isNegative ? "-" : "+"}${_formatTimeZoneOffsetISO(duration)}";
  }
  
  static String _formatTimeZoneOffsetISO(Duration duration) {
    duration = duration.abs();
    return "${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}";
  }

 ///get ISO time String from DateTime
 ///Output: 2020-09-16T20:42:38.629+05:30
  static String getCurrentISOTimeString({DateTime? dateTime}) {
    return formatISOTime(DateTime.now());
  }
}

@loonix
Copy link

loonix commented Nov 10, 2021

You have a big bug there, please remove the negative logic.
If you are in a timezone that is negative you will have double minus

  /// converts [date] into into string format
  /// Example:
  /// * Positive:   (UK)   `2020-09-16T11:55:01.802248+00:00`
  /// * Negative: (Canada) `2020-09-16T11:55:01.802248-08:00`
  static String formatISOTime(DateTime date) {
    var duration = date.timeZoneOffset;
    /// If the user is in Canada the time zone is GMT-8 then the signal will need to be negative.
    /// Because we already get the minus from the hours in the string then we don't need to add it to the string.
    /// In the case the timezone is GMT-0 or higher then the sign will need to be positive.
    var timezoneSignal = !(duration.isNegative) ? '+' : '';
    var dateString = (date.toIso8601String() + "${timezoneSignal}${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
    return dateString;
  }

@adimar-aot
Copy link

@loonix, the code will not have a double minus if it's using the absolute value of the duration (.abs()), but your suggestion would also work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment