0

How do I convert my current time into GMT/ UTC time in Dart, similar to the javascript way: new Date().toUTCString():

Tue, 01 Nov 2011 19:06:35 GMT

I tried couple of formatters see below but it did not work

//var f = DateFormat('E, d MMM yyyy HH:mm:ss z');
var f = DateFormat('E, d MMM yyyy HH:mm:ss'); 
    var date = f.format(DateTime.now());

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167

3 Answers3

3

Convert your current time to UTC and append " GMT" to the string.

import 'package:intl/intl.dart';

void main(List<String> arguments) async {
  var f = DateFormat('E, d MMM yyyy HH:mm:ss');
  var date = f.format(DateTime.now().toUtc()) + " GMT";
  print(date);
}


Using an arbitrary timezone (not just GMT/ UTC)

Although it is in the documentation, you can't create strings with arbitrary timezones with DateTime, as timezone support/ implementation has been missing, and is a GitHub issue since 2015. For example, you can't display a different timezone in the string, as using z in the format string doesn't work: callers get an UnimplementedError. See the 3 related GitHub issues: one, two and three.

UnimplementedError
#0      _DateFormatPatternField.formatTimeZone (package:intl/src/intl/date_format_field.dart:640:5)
#1      _DateFormatPatternField.formatField (package:intl/src/intl/date_format_field.dart:381:16)
#2      _DateFormatPatternField.format (package:intl/src/intl/date_format_field.dart:244:12)
#3      DateFormat.format (package:intl/src/intl/date_format.dart:276:26)
#4      main (file:///Users/benbutterworth/datetime/bin/datetime.dart:11:16)
<asynchronous suspension>
#5      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
  • var f = DateFormat('E, d MMM yyyy HH:mm:ss z') throws UnimplementedError. – Anuranjan Srivastav Oct 15 '20 at 11:18
  • That's because it's not yet implemented, see my edited answer – Ben Butterworth Oct 15 '20 at 11:32
  • 1
    I am converting javascript equivalent statement new Date().toUTCString() in to equivalent dart. I know there is utc method in dart but that does not produce this output. – Anuranjan Srivastav Oct 15 '20 at 11:34
  • Okay, so in this case, you can just append "GMT" after converting the current time to UTC/ GMT time. This is not a problem of "GMT", but a date conversion, which you can do [here](https://stackoverflow.com/a/26258061/7365866) and maybe [TimeZone package](https://pub.dev/packages/timezone) as well. – Ben Butterworth Oct 15 '20 at 11:47
  • I think next time, if you phrase your question with a little more information, I'd be able to help more quickly. I was interested in running dart without Flutter today though, so I am happy to help. – Ben Butterworth Oct 15 '20 at 11:48
2
var f = DateFormat('EEE, d MMM yyyy HH:mm:ss'); 
var date = f.format(DateTime.now());

This output date in format Tue, 01 Nov 2011 19:06:35, without GMT

savke
  • 268
  • 1
  • 3
  • 11
0

Finally I found out the work around as below

 String getUTCDate() {
    try {
      var f = DateFormat('E, d MMM yyyy HH:mm:ss');
      var now = DateTime.now().toUtc();
      return f.format(now) + ' GMT';
    } catch (e) {
      print('Error ******' + e.toString());
      throw e;
    }
  }
  • Your solution generally works, but it outputs single-digit days when JavaScript's new Date().toUTCString() always outputs double-digit days. Also, you can integrate the "GMT" literal string into the date format. Here is the code I'm using: `var f = DateFormat("E, dd MMM yyyy HH:mm:ss 'GMT'"); var now = DateTime.now().toUtc(); return f.format(now);` – Pi Da Oct 15 '20 at 20:01
  • Your original question said `I want to produce an output of date which includes the timezone of the caller`. Calling `.toUTC` includes the timezone of UTC/ GMT, not the timezone of the caller. – Ben Butterworth Oct 25 '20 at 18:38