flutterdartdatetimeflutter-intl

flutter, dart : How convert formatted DateTime to its original form


_date = DateFormat('dd-MMM-yyyy').format(_rawDateTime);

I have formatted the above _date with Intl package for displaying and now I have to send back to server and server require original format so how I can convert the above formatted date back into its original format. the server required format is 2022-08-19T09:12:15.406Z


Solution

  • You track DateFormat and use .parse on string.

    final formatter = DateFormat('dd-MMM-yyyy');
    final _date = formatter.format(_rawDateTime);
    final data = formatter.parse(_date);
    

    For utc

    final data = formatter.parse(_date).toUtc(); //2022-08-19 18:00:00.000Z
    

    Only date will be available from this because the formatter already parsed only date part here.