Back

[SOLVED] How to convert Appwrite datetime to Flutter datetime?

  • 0
  • Flutter
djcali
11 Apr, 2023, 05:41

Hi, I get back a datetime from appwrite in this format 2023-01-06T14:30:00.000+00:00 and I need to end up with a Flutter dateTime. I tried this String utcDateString = '2023-01-06T14:30:00.000+00:00'; DateTime utcDate = DateTime.parse(utcDateString); but it is not working. Any ideas?

TL;DR
The problem has been solved. The solution is to use the `DateFormat` class in Flutter to convert the Appwrite datetime to Flutter datetime. This function can be used: ```dart String formatDateTimeFromUtc(dynamic time) { try { return new DateFormat("yyyy-MM-dd hh:mm:ss") .format(new DateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(time)); } catch (e) { return new DateFormat("yyyy-MM-dd hh:mm:ss").format(new DateTime.now()); } } ``` Alternatively, a substring approach can also be used, but it is not recommended.
djcali
11 Apr, 2023, 05:43

I was able to get it working with use of substrings but seems like not a good way to do it. ```DateTime isoToFlutterDateTime({required String date}) { final reformat = '${date.substring(0, 4)}-${date.substring(5, 7)}-${date.substring(8, 10)} ${date.substring(11, 16)}';

final dateParse = DateTime.parse(reformat);

return dateParse; }```

Binyamin
11 Apr, 2023, 05:55

This function seems to do the trick

TypeScript
String formatDateTimeFromUtc(dynamic time){
  try {
    return new DateFormat("yyyy-MM-dd hh:mm:ss").format(new DateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(time));
  } catch (e){
    return new DateFormat("yyyy-MM-dd hh:mm:ss").format(new DateTime.now());
  }
}

From here https://stackoverflow.com/questions/58815575/how-do-i-format-date-like-this-2019-07-08t103728z-in-flutter

Drake
11 Apr, 2023, 06:13

What's wrong with the parse?

djcali
11 Apr, 2023, 06:29

Sorry the parse method is working. I was doing something else wrong. 😆

joeyouss
11 Apr, 2023, 08:05

Hi, @djcali do you need any more help regarding this

djcali
11 Apr, 2023, 14:43

No thanks! problem solved.

Drake
11 Apr, 2023, 18:19

[SOLVED] How to convert Appwrite datetime to Flutter datetime?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more