flutter run
flutter run

Reputation: 65

Flutter How to format the date and time pull from the database

Good day i want to format my date 2020-11-05 to 11-05-2020 my database type is timestamp

[![Here is my view ][1]][1] [1]: https://i.sstatic.net/sOgYN.jpg

//this is my my litsview

   @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: RefreshIndicator(
          onRefresh: getData,
          key: _refresh,
          child: loading
              ? Center(child: CircularProgressIndicator())
              : ListView.builder(
                  itemCount: list.length,
                  itemBuilder: (context, i) {
                    final x = list[i];
                    debugPrint(x.toString());
                    return ExpansionTile(
                      title: ListTile(
                          title: Text(
                              'Transaction ID:\t' +
                                  x.transaction_id +
                                  '\nVesselname:\t' +
                                  x.vesselname +
                                  '\nVoyage #:\t' +
                                  x.voyageno,
                              style: TextStyle(
                                  fontSize: 15.0, fontWeight: FontWeight.bold))),
                      children: <Widget>[
                        ListTile(
                            title: Text('Ship Call #:\t' + x.scn,
                                style: TextStyle(fontSize: 15.0))),

//here is my value anchorage estimated time of arrival i want to format it into 11-20-2020

  ListTile(
             

                        title: Text(
                        'Anchor Estima Time of Arrival :\n' + x.anchor_eta,
                        style: TextStyle(fontSize: 15.0))),
          

                   ListTile(
                    title: Text(
                        'Anchor Actual Time of Arrival :\n' + x.anchor_ata,
                        style: TextStyle(fontSize: 15.0))),
             
              ],
            );
          },
        ),
));

} }

Upvotes: 1

Views: 2421

Answers (2)

Tipu Sultan
Tipu Sultan

Reputation: 1865

You can use this function for formatting your date

String formatDate(String dbDate){
  List<String> dateTimeList = dbDate.split(" ");
  List<String> dateList = dateTimeList.first.split("-");
  String dateTime = dateList[1]+"-"+dateList.last+"-"+dateList.first + " "+ dateTimeList.last;
  return dateTime;
}

Upvotes: 3

dotmp3
dotmp3

Reputation: 494

Hi flutter run(cool name btw),

There are two ways you can do achieve this:

  1. Use intl library:
String correctlyFormattedDateTime(DateTime date){
   return DateFormat('dd-MM-yyyy').format(date); //=> 02/07/2020
}
  1. Edit the date directly:
String correctlyFormattedDateTime(DateTime date){
   return '${date.day}/${date.month}/${date.year}'; //=> 02/07/2020
}

Here is a duplicate of this question

Please leave a comment if you need any further help!

Upvotes: 0

Related Questions