error when trying to change DateTime to string that has time zone format in dart

716 views Asked by At

so I use intl package. here is my code

import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';

String toString(DateTime originalDate) {
    initializeDateFormatting();
    final DateFormat formatter = DateFormat("EEEE, d MMMM. HH:mm zzz", "id");
    final String formatted = formatter.format(originalDate);
    return formatted;
  }

as you can see, I am trying to make DateTime as a string using "EEEE, d MMMM. HH:mm zzz" format. so I expect I will have the result date string like this:

Senin, 24 Mei. 18:00 WIB.

but I have error whenever I have that zzz in the format. if I remove the zzz then it should be fine.

I guess I need to put a TimeZone ID in my code above. because I has this unimplemented error in date_format_field.dart file

String formatTimeZone(DateTime date) {
    throw UnimplementedError();
  }

but I don't know how to set the timezoneID to my code. my TimeZone ID is "Asia/Jakarta"

I am okay if you have different code or not using intl package at all. as long as I can specify the timezone and the locale ID

1

There are 1 answers

0
flutter_bee On

duplicate of Dart - How to format DateTime based on device timezone?

try using this:

String toString(DateTime originalDate) {
    initializeDateFormatting();
    final DateFormat formatter = DateFormat("EEEE, d MMMM. HH:mm zzz").parseUTC(dateTime).toLocal();
    final String formatted = formatter.format(originalDate);
    return formatted;
  }